This is a creation in Article, where the information may have evolved or changed.
Here are the five evolutionary stages of GO programmers:
The first stage (vegetable forcing): just learned the language. Some tutorials or workshops have been used to understand the basic syntax and can write short snippets of code.
Second Stage (explorer): Can write a complete program, but do not understand some of the more advanced language features, such as "channels". You haven't used go to write a big project.
The third stage (Big hand): You can skillfully use go, can use go to solve, production environment in a specific and complete problem. Has formed a set of its own idioms and common code base. Go is a very useful tool in your coding scheme.
Stage four (great God): It is absolutely impossible to understand the design choices and motivations behind the go language. A concise and composable philosophy that can be understood.
Preacher: Actively share with others about go language knowledge and your understanding of the go language. In a variety of suitable places to send their own voice, participate in mailing lists, set up QQ Group, do special reports. Being a preacher is not necessarily a completely independent phase, and this role can be in any one of these stages.
First stage: Vegetable forcing
Rookie at this stage use go to create some small projects or toy projects. They should take advantage of the Go tour,go playground, go docs, and mailing lists (golang-nuts).
Func Main () {FMT. Println (Stringutil. Reverse ("!selpmaxe OG, Olleh"))}
This is a simple example of the go language, and this snippet is from the hello.go in the Golang/example code base . Click to view full code.
An important skill, the newcomer should try to learn how to ask questions correctly. Many new people in the mailing list say, "Hey, this is an error," which does not provide enough information to allow others to understand and help them solve the problem. What others see is a post with hundreds of lines of code pasted, and no effort is devoted to highlighting the problem.
Therefore, you should try to avoid pasting the code directly into the forum. Instead, use the "Share" button in the Go playground that can be edited and run directly in the browser to link to the code snippet.
Phase 2:the Explorer
Explorers can already use go to write small software, but sometimes they are still confused. They may not fully understand how to use the advanced features of go, such as channels. Although they have a lot of things to learn, but have mastered enough to do something useful! They start to feel about the potential of go and are excited about what they can do with go.
There are usually two steps to be experienced during the exploratory phase. First, the inflated expectations reach the top, and you think you can do all the things with go, but you can't understand or comprehend the true meaning of go. You'll probably write the go code in familiar language patterns and idioms, but there's no strong sense of what it's like to go. You start trying to do things like "Migrating architecture x, from Y to go".
After reaching the vertex of the expected swelling, you will encounter the valley of ideal disillusionment. You start to miss the character X of language y, and you don't have a complete grasp of the go. You're still writing the Go Language program in other programming languages, and you're even starting to feel frustrated. You may be using both reflect and unsafe packages, but this is not a typical go. The true go does not use the same magical things.
A good example of the project generated during this exploratory phase is the Martini Web framework. Martini is an early web framework for the Go language, which absorbs a lot of ideas (such as dependency injection) from the Ruby Web framework. Initially, the framework had a strong response in the community, but it was increasingly criticized for performance and usability. Jeremy Saenz, author of the Martini Framework, responds positively to these feedback from the go community and writes a library that is more compliant with the Go Language Specification Negroni
Func (M *martini) runonaddr (addr string) {//todo:should probably be implemented using a new instance of HTTP. Server in place of//calling HTTP. Listenandserver directly, so that it could is stored in the Martini struct for later use. This would also allow to improve testing when a custom host and port is passed. Logger: = M.injector.get (reflect. TypeOf (M.logger)). Interface (). (*log. Logger) Logger. Printf ("Listening on%s (%s) \ n", addr, Env) logger. Fatalln (http. Listenandserve (addr, m))}
An interactive code snippet from the Martini Framework, which is an example of an unusual go. Note Dependency injection implemented with reflection packets
Func testnegroniservehttp (t *testing. T) { Result: = "" response: = Httptest. Newrecorder () N: = New () n.use (Handlerfunc (func (RW) http. Responsewriter, R *http. Request, next http. HANDLERFUNC) { result + = "Foo" Next (rw, r) result + = "ban" }) N.use ( Handlerfunc (func (rw http. Responsewriter, R *http. Request, next http. HANDLERFUNC) { result + = "Bar" Next (rw, r) result + = "baz" }) N.use ( Handlerfunc (func (rw http. Responsewriter, R *http. Request, next http. HANDLERFUNC) { result + = "bat" rw. Writeheader (http. StatusbadrequeST) }) n.servehttp (response, *http. Request) (nil)) expect (T, result, "Foobarbatbazban") expect (T, response. Code, http. Statusbadrequest)}
An interactive code snippet from the Negroni library, which is an example of an authentic go
Other languages often rely on third-party libraries when it comes to providing some core functionality, such as HTTP processing. But the go language is very different at this point, and its standard library is very powerful. If you think the Go standard library is not strong to do what you want to do, then I say you are wrong. The Go Language standard library is incredibly powerful, and it's worth taking the time to read its code and learn the patterns it implements.
Func (SRV *server) listenandserve () error {addr: = srv. Addr if Addr = = "" {Addr = ": http"} ln, err: = Net. Listen ("tcp", addr) if err! = Nil {return err} return srv. Serve (Tcpkeepalivelistener{ln. ( *net. TcpListener)})}
The Listenandserve function fragment in the Go standard library. If you have written a go program, you may have called this function many times, but have you ever taken the time to see its implementation? Click on the code snippet above.
Disillusionment in the depths of disillusionment comes from the fact that you are still thinking in other language patterns, and you have not fully explored what go can offer you. Here are some fun things you can do to break down the dilemma and further explore the fun things in this language.
Go generate
Now look at go generate. Go generate is a command that you can use to automate your own go code. You can combine meta-programming such as jsonenums (a class library that automatically generates JSON marshalling boilerplate code for enumeration types) to quickly automate the writing of repetitive tedious code using go generate. There are already a large number of interfaces that can be used to parse the AST in the Go Standard class library, and AST makes it easier and easier to write meta-programming tools. At the meeting, there were two other discussions (meta-programming practices in the go language and embracing the standard class library).
Func main () { flag. Parse () If Len (*typenames) = = 0 { log. Fatalf ("The Flag-type must be set") } types: = Strings. Split (*typenames, ",") //Only one directory at a time can processed, and the default is ".". dir: = "." if args: = flag. Args (); Len (args) = = 1 { dir = args[0] } else if Len (args) > 1 {&nb sp; log. Fatalf ("Only one directory at a time") } pkg, err: = parser. Parsepackage (dir, *outputsuffix+ ". Go") if err! = Nil { Log. Fatalf ("Parsing package:%v", err) } var analysis = struct { command string packagename string typesandvalues map[string][]string }{ command: strings. Join (OS. Args[1:], ""), packagename: pkg. name, Typesandvalues:make (map[string][]string), } //Run generate for each type. for _, TypeName: = Range Types { &nbs p; values, err: = pkg. Valuesoftype (typeName) if err! = Nil { log. Fatalf ("Finding values for type%v:%v", TypeName, err) } & nbsp; analysis. Typesandvalues[typename] = values var buf bytes. buffer If Err: = Generatedtmpl.execute (&buf, analysis); Err! = Nil { log. Fatalf ("Generating Code:%V", err) } src, err: = format. Source (BUF. Bytes ()) if err! = Nil { //Should never happen, but can arise if developing this CODE.      &N bsp; //The user can compile the output to see the Error. &nbs p; log. Printf ("Warning:internal error:invalid Go generated:%s", err) log. Printf ("Warning:compile the package to analyze the error")   &Nbsp; src = buf. Bytes () } Output: = Strings. ToLower (typeName + *outputsuffix + ". Go") outputPath: = filepath. Join (dir, Output) if err: = Ioutil. WriteFile (OutputPath, SRC, 0644); Err! = Nil { log. Fatalf ("Writing output:%s", err) } }}
An interactive fragment demonstrates how to write the Jsonenums command.
Opengl
Many people use go as a Web service, but do you know that you can also write cool graphics apps with go? Check out the bundle of go in OpenGL.
Func main () { GLFW. Seterrorcallback (errorcallback) if!GLFW. Init () { Panic ("Can ' t init glfw!") } defer GLFW. Terminate () window, err: = GLFW. CreateWindow (Width, Height, Title, nil, nil) if err! = Nil { Panic (Err) } window. Makecontextcurrent () GLFW. Swapinterval (1) gl. Init () If err: = Initscene (); Err! = Nil { fmt. fprintf (OS. Stderr, "Init:%s\n", err) return } Defer Destroyscene () for!window. Shouldclose () { drawscene () Window. Swapbuffers () GLFW. Pollevents () }}
The interactive fragment is a description of the OpenGL bundle of go to make Gopher cube. Click on the function or method name to explore.
Hacking Marathon and challenges
You can also watch the challenges and Hackathon Marathon, similar to Gopher Gala and go challenge. In the past, programmers from all over the world have been working together to challenge some real cool projects where you can get inspiration from them.
Stage Three: Veteran
As an old hand, this means you can solve many of the issues you care about in the go language. The new problems that need to be solved will bring new questions, and after trial and error, you have learned what can and cannot be done in this language. At this point, you have a solid understanding of the language's habits and patterns. You can work very efficiently, write readable, well-documented, maintainable code.
A good way to be a veteran is to work on a big project. If you have an idea for a project yourself, start doing it (and, of course, make sure it doesn't already exist). Most people may not have the idea of a big project, so they can contribute to a project that already exists. The go language already has many large projects, and they are being used extensively, such as Docker, Kubernetes and go itself. Take a look at this list of items
Func (CLI *dockercli) Cmdrestart (args ... string) error { cmd: = CLI. Subcmd ("Restart", "CONTAINER [CONTAINER ...]", "Restart a running CONTAINER", true) nseconds: = cmd. Int ([]string{"T", "-time"}, Ten, "Seconds to wait for stop before killing the container.") cmd. Require (flag. Min, 1) Utils. Parseflags (cmd, args, true) V: = URL. values{} v.set ("T", StrConv. Itoa (*nseconds)) var encounterederror error for _, Name: = Range cmd. Args () { _, _, Err: = Readbody (Cli.call ("POST", "/containers/" +name+ "/ Restart? " +v.encode (), nil, false)) If err! = Nil { FMT. fprintf (Cli.err, "%s\n", err) Encounterederror = Fmt. Errorf ("error:failedTo restart one or more containers ") } else { & nbsp; FMT. fprintf (Cli.out, "%s\n", name) } } Return Encounterederror}
The interactive code snippet for the Docker project. Click on the function name to begin the journey of discovery.
Veteran should have a strong grasp of the tools of the go ecosystem, as these tools really improve productivity. You should understand go Generate,go vet,go test-race, and Gofmt/goimports/goreturns. You should use Go FMT because it will automatically format your code according to the style standard of the go community. Goimports can do the same thing, and it will add the lost imports. Goretures not only did what was said before, but also added the missing error in the return expression, which everyone hated.
At the veteran stage, you must start code review. The meaning of code review is not to modify or find errors (that's what testers do). Code review can help maintain a unified programming style, improve the overall quality of your software, and improve your own programming skills in other people's feedback. Almost all large open source projects do code review for each commit.
Here's an example of learning from human feedback: Google's go team used to declare command-line tags outside of the main function. At last year's Gophercon meeting, Francesc met the SoundCloud company's Peter Bourgon (@peterbourgon). Peter Bourgon says in SoundCloud, they all declare tags inside the main function so they don't mistakenly use tags externally. Francesc now thinks this is a best practice.
Stage four: Experts
As an expert, you have a good understanding of the philosophical thinking of language. For the Go language feature, you know when to use and when not to use. For example, Jeremy Saenz in the Dotgo storm discussion about when to use an interface.
Func (client *client) Go (Servicemethod string, args interface{}, reply interface{}, done Chan *call) *call {  &N Bsp Call: = new (call) call. Servicemethod = servicemethod call. Args = args call. Reply = reply If done = nil { done = make (Chan *call, 10)// buffered. } else { //If caller passes done! = nil, it must AR Range that //done have enough buffer for the number of simultaneous //RPCs that'll be using this channel. If the channel   ; //is totally unbuffered, it's best not-to-run at all. if cap ) = = 0 { log. Panic ("Rpc:done channel is unbuffered") } } call. Done = done client.send (call) return call}
A small piece of interactive code fragments from the standard class library use the channel. Understanding the decision behind the model in the standard class library is the only way to become an expert.
But don't be an expert in a single language. Like any other language, go is just a tool. You should also explore other languages and learn about their patterns and styles. Francesc from his experience of using go to find the inspiration to write JavaScript. He also likes to focus on immutability and the Haskell language, which is committed to avoiding variability, and gets inspiration for how to write go code.
Preacher
As a preacher, you share your knowledge, impart what you learn and best practices that you put forward. You can share the places you like or dislike about go. Go meetings all over the world to find the closest to you.
You can be a preacher at any stage, and don't wait until you become an expert in this field to make your own voice. At any stage of your learning go, ask questions, combine your experience to give feedback, and don't be shy about making places you don't like. Your feedback can help the community improve the way it does things, and it may change your own view of programming.
Func main () { httpaddr: = flag. String ("http", "127.0.0.1:3999", "HTTP service address (e.g., ' 127.0.0.1:3999 ')") originhost: = flag. String ("Orighost", "", "host component of the Web Origin URL (e.g., ' localhost ')") flag. Stringvar (&basepath, "base", "", "Base path for slide template and static resources") flag. Boolvar (&present. Playenabled, "Play", true, "Enable playground (permit execution of arbitrary user Code)") nativeclient: = Flag. Bool ("NaCl", False, "use Native Client environment Playground (prevents non-go code execution)") flag. Parse () if basepath = = "" { p, err: = Build. Default.import (Basepkg, "", build. findonly) If err! = Nil { FMT. fprintf (OS. Stderr, "couldn ' t find gopresent files:%v\n", err) &NBsp; FMT. fprintf (OS. Stderr, Basepathmessage, basepkg) OS. Exit (1) } basepath = P.dir } Err: = Inittemplates (basepath) if err! = Nil { log. Fatalf ("Failed to parse Templates:%v", err) } ln, err: = Net. Listen ("TCP", *HTTPADDR) if err! = Nil { log. Fatal (ERR) } defer Ln. Close () _, port, err: = Net. Splithostport (Ln. Addr (). String ()) if err! = Nil { log. Fatal (Err) } origin: = &url. Url{scheme: "http"} if *originhost! = ""{ origin. Host = net. Joinhostport (*originhost, port) } else if Ln. Addr (). (*net. TCPADDR). Ip. Isunspecified () { name, _: = OS. Hostname () origin. Host = net. Joinhostport (name, port) } else { reqhost, reqport, err: = Net. Splithostport (*HTTPADDR) if err! = Nil { log. Fatal (ERR) } if reqport = = "0" { origin. Host = net. Joinhostport (Reqhost, port) } else { origin. Host = *httpaddr }  &NBsp } if present. playenabled { if *nativeclient { sockets. runscripts = false socket. Environ = func () []string { IF runtime. Goarch = = "AMD64" { return environ ("Goos=nacl", "GOARCH=AMD64P32") } return environ ("Goos=nacl") } } PlayScript ( BasePath, "Sockettransport") http. Handle ("/socket", socket. Newhandler (Origin) } http. Handle ("/static/", http. Fileserver (http. Dir (BasePath))) if!ln. Addr (). (*net. TCPADDR). Ip. Isloopback () && present. playenabled &&!*nativeclient { log. Print (localhostwarning) } log. Printf ("Open your Web browser and visit%s", Origin. String ()) log. Fatal (http. Serve (ln, nil))
The main function of the
Popular present command is used by many go users to make slideshows. Many of the speakers modified the module to suit their needs.