Go-tour Source Reading

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Go-tour is a Web project that is highly readable.

Let's take a look at the catalogue below.

AppEngine: This package is used when you want to put Go-tour on the Gae, we just use the local installation, so the code here can not look

Solutions: This folder can be ignored, only the execise answer in the document, not related to Go-tour site

Talks: This is the author's speech about Go-tour put it in here, can also directly ignore

Static: This folder is the storage of resources, js,css,html are placed in the face

Tree: This folder store program automatically generates a tree of code packages. This is a specific function to use, not related to the site

WC: Test Kit. This is a specific exercise in the use of, not related to the site

Pic: The processing package for the picture. This is a specific exercise in the use of, not related to the site

Gotour: This is the entrance to the real Go-tour, and the main bag is here.

Look from Gotour/local.go

Two parameters, HTTP and HTML, were written directly in the global variable.

The HTTP description is on which port to listen.

HTML is a description of the output HTML page or open, this HTML is useless, it is estimated that the author wants to do this function, has not been finished.

The init function is to set the temporary folder where the generated executable files will be compiled.

into the main function, there is a uniq Chan int, this is for the temporary file to do a unique indicator, will continue to grow.

The Uniq in main is an ID generator, and this method is very common

VAR (      //A source of numbers, for naming temporary files     uniq = make (chan int)) Func main () {      flag. Parse ()     //source of unique numbers     go func () {for          I: = 0;; i++ {               uniq <-i          }     } ()}

Main first gets the directory of Go-tour, which is obtained using the build package and can be learned as a fixed template

Find and serve the Go tour files p, err: = Build. Default.import (Basepkg, "", build. FINDONLY) If err! = Nil {    log. Fatalf ("couldn ' t find Tour files:%v", err)}root: = P.dir

The following is the use of HTTP packets to make different routes.

There are several different routers:

/,/favicon.ico,/static/, talk/, kill

Kill is a command that kills a running. There's a running here to run a running program.

The run function is a good example of how to run a system command and output the result

OK, here's the main look, when you actually run, you will find that the code in the program is actually compiled using/compile and/fmt two kinds. Both of these access paths are defined in Goplay.go and Fmt.go

See Goplay.go first.

The Handlefunc is written directly in Init (), which also makes it more visible to everyone that the package is used for compiling

The specific implementation steps in the compile function are implemented using the Compile function (Local.go), followed by the JSON-encoded output.

Here's a look at the Compile,main in main to maintain a running structure, it is worth noting that its use of initialization directly in the struct definition of the time to add a Var, this can be remembered

var running struct {

Sync. Mutex

CMD *exec. Cmd

}

Keep looking at compile, this time, the parameter request contains the go code to run

First put the go code in the temporary file, and then bin is the target generation file, SRC is the source file

The first step is to write the code into SRC

Second step, run go build and use the-o parameter to generate the target file into the bin

The third step, run the bin, output (of course, this result is to do some processing, such as the role of Commentre)

Fourth step, delete the src and bin two files

Here's a look at fmt.go.

Formatting the code should also be possible with the Go FMT tool, but this is not the way to use it.

As with Goplay.go, a handle was set up at Init.

Then use the GOFMT function to format the code, into the GOFMT function

The GOFMT function uses Go/token,go/parser to format the code, and the actual work is:

1 Sequential Import Package

2 Beautify Go Code

Haha, this is to say that the GO program can be self (Go program) to beautify and run (bootstrap), this feeling is too good, use is go/xxx bag

Here are a few key functions:

Token. Newfileset (): Create Fset structure

Parser. Parsefile (): Parse The Go code (this is how the last mode can be resolved to what extent)

Ast. Sortimports (): Sort the Import Package

Printer. Fprint (): Landscaping Tools

This is the end of the code, as the page appears on the effect of paging, and so on, are the result of JS and CSS, and server-independent.

The entire page requires only 3 code files (except static pages)! 300 lines of code less than the amount! (Of course it more operation is in JS) less that is very much!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.