Go Core Development Study Notes (a) - the basic concept of the Go language

// The following contains the main function, you must use the main package, under one folder can have only one file using the main bag, other use will complain, is to
// create a new folder, so that different files main package, put in different folders.

// import package must be used in the program, if you do not use the compiler can not pass.
// function to specify the format is written so the braces, the brackets alone can not write a separate line.

package main
import "fmt"
func main() {
	fmt.Println("hello world!")
}

Language execution flow analysis:
program execution is divided into go run xx.go and go build xx.go two kinds of differences are as follows:

go run xx.go: run the source code, compile process is performed again after transplantation, we must ensure that the locale is also installed on the machine go after transplantation.

go build xx.go: compiling the source code, a N times larger than before the source file is produced, exe or bin, this file contains all dependent files necessary to run the program, the file need not be portable to other machines go locale to perform.

#go build -o <new_name> .go xx.go // This command can be given a new name for the file compiled.
#gofmt -w xx.go // this command to go their own messy format source file formatted according to the standard indented, -w to save, make the code more beautiful.

Go program development notes:

  1. Source documents are ending .go, this is a must.
  2. Go execution entry application is main () function, entry of all programs, and the C, java same.
  3. Strictly case-sensitive, and py the same.
  4. Each row of Go braces automatically add a semicolon, write do not write does not affect the result of the program, but do not write for aesthetic semicolon.
  5. Go compiler is compiling a row, do not write to the multi-line command line this boring thing.
  6. And import variables not used in the program pkg, unable to compile the code, is designed for aesthetic simplicity.
  7. Note that braces function written after the left parenthesis as line, without flowing variety of writing, only a fixed die func xxx () {...}

Escape character Description:
\ the n-: newline
\ t: the Tab
\ xxxx: reserved * any character itself, meaning
\ r: Under Enter (this highlights, carriage return and line feed are two different things children, this is the carriage return line of the Bank first, what is the carriage return character retains much character Enter on the front cover)

Standardized coding style:

  1. Go official recommended line comments to annotate the entire methods and statements.
  2. Goland IDE to use the Go language learning,
  3. Select all code can be indented using shift + tab to the left, use the tab can be indented to the right, you can use gofmt -w xx.go implementation code standard format.
  4. Operators generally customary maintain beautiful spaces on both sides, e.g. var ss = 1 or var ss = 2 + 4 * 5, this is not a mandatory requirement.

Documentation API (Application Programming Interface):
Go in the package of each function, see the package in the official website of each function (method?).
Using domestic websites: https://studygolang.com/pkgdoc .

Golang call a function of the way:
Import <pkg>
// <pkg> src file that is itself installed in GOROOT folder, each folder is a package (pkg)
// package is divided into many files go, are a variety of functions, it is to use. ( "args")

Published 50 original articles · won praise 18 · views 4029

Guess you like

Origin blog.csdn.net/weixin_41047549/article/details/89531987