Golang的特色介紹

Posted by Kubeguts on 2018-05-13

The benefit of Golang is as follows:

1. Quick compile time than the C , C++

Golang provides lightning-quick compiler by using a smart compiler and simplified dependency resolution algorithm.
When build the golang program, the compiler only looks at the libraries that you directly include. (C,C++,JAVA. those programming language will traverse entire dependency chain…)

2. Concurrency

go-routine

Golang can create go-routine, which is like thread but use far less memory and require less code to use.

Golang can create many go-routine on a single thread.

1
2
3
4
5
func log(msg string){
... some logging code here
}
// Elsewhere in our code after we've discovered an error.
go log("something dire happened")

channel

channel are data structure that let you send typed messages between goroutines with sychronization built in.

Channel can also avoid the share memory problem in concurrency modification problem.

Warning: channel isn’t provide data access protection.

3. Go type system.

Golang provides flexible hierachy-free type free system that enables code reuse with minimal refactoring overhead.

In tradictional OOD, like JAVA, need to implenment all the method declared in interface.

1
2
3
4
interface User {
public void login();
public void logout();
}

But in golang type system, you only implement the method you would use.

1
2
3
type Reader interface {
Read(p []byte) (n int, err error)
}

4. Memory management

Golang has modern garbage collector that does hard work for you.