This is a creation in Article, where the information may have evolved or changed.
"Disclaimer: Copyright, welcome reprint, please do not use for commercial purposes. Contact mailbox: feixiaoxing @163.com "
In fact, with the basic syntax of the previous article, we can do some simple things, such as file server. Maybe we don't believe it, it's okay. The following starts with code to illustrate the problem, in fact the entire code content will not exceed 10 lines.
Package Mainimport "Net/http" Func Main () { h: = http. Fileserver (http. Dir ("/Home")) http. Listenandserve (": 8888", h)}
With the code above, enter go run share.go directly. With so much to say, you can continue to see how advanced apps in the Go language are used.
(1) Concurrent Operation
Package Mainimport "FMT" import "Time" Func Show () {for { FMT. Print ("Child"); Time. Sleep (10000) }}func main () { go Show () for { FMT. Print ("parent") time . Sleep (10000) }}
(2) channel communication
Package Mainimport "FMT" func Show (c Chan int) {for { data: = <-C if 1 = = data { FMT. Print ("Receive") } }}func Main () { c: = make (chan int) go Show (c) for { C <-1 fmt. Print ("Send") }}
(3) Multi channel access
Package Mainimport "FMT" import "Time" Func Fibonacci (c, quit Chan int) { x, y: = 1, 1 for { Select {case C <-x: x, y = y, x+y case <-quit: fmt. Println ("Quit") return } }}func Show (c, quit Chan int) {for I: = 0; i <; i + + { FMT. Println (<-c) } quit <-0}func main () { Data: = Make (chan int.) Leave: = make (chan int) go s How to (data, leave) go Fibonacci (data, leave) for {time . Sleep (+) }}
< br>