Magic go language (advanced application), go Language
[Disclaimer: All Rights Reserved. You are welcome to reprint it. Do not use it for commercial purposes. Contact Email: feixiaoxing @ 163.com]
In fact, with the basic syntax of the previous article, we can do some simple things, such as file servers. Maybe you don't believe it. It's okay. The following code is used to describe the problem. In fact, the entire Code contains no more than 10 lines.
package mainimport "net/http"func main() { h := http.FileServer(http.Dir("/home")) http.ListenAndServe(":8888", h)}
With the above Code, enter go run share. go directly. After talking about this, you can continue to see how advanced applications 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 < 10; i ++ { fmt.Println(<- c) } quit <- 0}func main() { data := make(chan int) leave := make(chan int) go show(data, leave) go fibonacci(data, leave) for { time.Sleep(100) }}
Does the go language support table-level application development? (Compilers)
Go can develop desktop applications, but it is not very comfortable.
The following GUI Libraries are available:
1. The binding between Go and QT, produced by goqt and LiteIDE, has not yet been released
2. go. uik, a concurrent UI tool implemented by pure Go
3. walk, Windows Application Library Kit
4. gform, Windows GUI framework
Currently, the number of walks is relatively large.
However, the go GUI library is not as handy as C # and C/C ++.
This problem should be improved soon. After all, the demand for using Go to develop desktops is increasing.
Currently, I am using go http as the backend, Webkit + HTML5 as the interface, which is expressive, and the front-end does not need to learn new knowledge. It can be done in general management applications.
Channel problems in the go Language
First, you should add "| OS. O_WRONLY" when opening the file"
File, err: = OS. OpenFile ("data. dat", OS. O_CREATE | OS. O_APPEND | operating. O_WRONLY, 0777)
The second problem is to put "ch <-I" in the Count method into the last row of the method.
Because once "ch <-I" is executed, "<-ch" in the main method is executed. When all "<-ch" is executed, the program ends. However, at this time, the Count thread method is too late to complete (the file opening speed is relatively slow), so only one file operation is completed.