Go Language learning Note one
--------------------------------------------------------------------------
Advantage:
1. It is a system-level language, statically compiled, and is a C-series language.
2. There are many built-in libraries that are very similar to Python.
3. Syntax is simple enough, the introductory learning cost is very low, for me to switch from PHP and Python people.
4. Fast, take a simple page, I use PHP development concurrency can reach 500 very good, but with go easy to tens of thousands, which is incomparable performance
And the efficiency of go development is about the same as PHP.
5. From Google hand, and a bunch of cattle in the maintenance, based on BSD open source, community active.
--------------------------------------------------------------------------
Disadvantages:
1. Some libraries are immature, as is the case with processing.
2.cgo under the window System compile very troublesome, take the SQLite database driver, in the window under the compiler will encounter a lot of trouble.
3.runtime is not mature enough, GC is not very good, but heard that go 1.1 version will have a relatively large performance improvement.
4.Go of open-source projects is not enough. I think the development of a language is not only the language itself, but also the promotion of big companies or good projects.
--------------------------------------------------------------------------
Open Source Projects
The open source project gives me a lot of confidence and a few open source systems:
Vitess (YouTube's database proxy system),
NSQ (a real-time information processing system for bitly),
Skynet (a lightweight Distributed service framework),
Seven Cattle company all with go development,
360 developed class imessage application, support tens of thousands of users, while a single server long connect 80w,
These systems are all running on their own lines, which gives me greater confidence to use go to develop high-performance, high-stability applications.
--------------------------------------------------------------------------
Why is go called the Internet Era C?
I think go is supporting concurrency at the language level, and using the simple keyword go can make the most of multicore, a time when hardware is evolving, so simple
A single can take full advantage of the hardware multicore, this is how important a feature Ah! But go also lacks some high-quality third-party packages compared to C, such as
OpenGL and so on, so the inside of go also supports the code written directly in the C language with CGO.
At the same time, I have developed two open source projects:
Beego: A Go development framework that mimics Python's tornado system development, and several systems are now developed based on that framework.
Beedb: A Go Language ORM library that can manipulate database data as if it were a struct. At the moment, the API interface inside of us is using this ORM.
Developed.
1. Book "Go Language Programming"
1.1 Go language is a statically compiled language. Compilation is very fast, significantly faster than c,c++.
The official compiler for the go language is the GC.
View official documents: Run Instructions godoc-http=:8000 open http://localhost:8000 in the browser to see the official go language
File.
The go language supports calling external C-language code in the program as a CGO tool.
1.2 Edit
Both the Go language keyword and the operator use ASCII-encoded characters, but the identifier in the Go language enables any Unicode-encoded string, so the go language
Developers are free to use their native language in code.
Compiling
The Go language compiles faster, so the go language can be used as a #! on Unix-like systems Script to use. The #!/usr/bin/env Gonow or
#!/usr/bin/env Gorun is added to the beginning of the. go file where main () is located.
。
Example
www.qtrac.eu/gobook.html Get all the source code of this book.
environment variable settings
Add the following line to the. bashrc file:
export goroot= $HOME/opt/go
export paht= $PATH: $GOROOT/bin
Http://www.qtrac.eu/gobook-1.0.zip
gofmt-w SRC can format the entire project
How do I view the corresponding package document?
If it is a builtin package, then execute Godoc builtin
If it is an HTTP packet, then execute Godoc net/http
If you look at a function inside a package, execute Godoc fmt printf, or you can view the corresponding code to execute GODOC-SRC FMT printf
-----------------------------------
UTF-8 natively supports utf-8 strings and identifiers, because Utf-8 's inventor is also the inventor of the go language.
Concurrent
Goroutine is the core of the go language parallel design, Goroutine is the thread, but it is smaller than the thread, and more than 10 goroutine may
At the bottom is five or six threads, and the go language internally helps you realize the memory sharing between these goroutine.
The execution goroutine requires minimal stack memory (4--5k).
By default, the scheduler uses only a single thread. To play parallel with multicore processors, you need to show the call in our program
Runtime. Gomaxprocs (N) tells the scheduler to use multiple threads at the same time.
Channel
1. Non-buffered channel
By default, channel receive and send data is blocked, unless the other end is ready, which makes Goroutine synchronization more simplified
Without the need for an explicit lock.
The so-called blocking, it is said that if read, it will be blocked until the data received, any transmission will be blocked until the read data is read out.
The Unbuffered channel is the best tool for synchronizing between multiple goroutine.
2. Buffered Channel
CH: = Make (chan bool, 4) Creates a bool-type channel that can store 4 elements. In this channel, the first 4 elements can have no
A blocked write. When the 5th element is written, the code blocks until the other goroutine reads some elements from the channel, freeing up space.
Range and close
Func Test () {
C: = make (chan int, 10)
Go Fibonacci (Cap (c), c)
for I: = range C {//channel with range action cache type
FMT. Println (i)
}
}
Func Fibonacci (n int,c Chan int) {
x, y: =
for I: = 0; i < n; i++ {
c <-x
x, y = y,x+y
}
Close (c)//closes the channel at the producer's place, rather than closing it in the place where it is consumed, so it is easy to panic.
}
Select
Func Test () {
C: = make (chan int)
Quit: = make (chan int)
go func () {
for I: =0; i<10;i++{
FMT. Println (<-C)
}
quit <-0
}()
Fibonacci (c,quit)
}
Func Fibonacci (c,quit Chan int) {
x, y: =
for {
Select {//*select is blocked by default and will only run if it is sent or received in the channel that is being listened to.
Case C <-x://* When multiple channel is ready, select is randomly selected for execution.
x, y = y,x+y
Case <-quit:
FMT. Println ("Quit")
return
Default:
FMT. Println ("Do some Thing");
}
}
}
Timeout
Func Test () {
C: = make (chan int)
o: = Make (chan bool)
go func () {
for {
Select {
Case V: = <-c:
FMT. Println (v)
case <-time. After (5 * time. Second) :
FMT. PRINTLN ("timeout")
o <-True
Break
}
}
}()
<-o
}
Go Language learning Note one