Go Bible Notes--Chapter I.

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

1.1
1) The code for the Go language is organized through the package, which contains one or more. Go End source code files.


2) Each source file is preceded by a declaration statement from a package xxx.


3) Each source file will import multiple uses of the package.


4) The package main is a special package that defines a separate program that can be run, rather than a library as in other packages. In main, the main function is also a special function, which is the entrance to our entire program.


5) The semicolon is not required to end as a statement or declaration, and is added automatically unless you want to separate statements and declarations in one row.


6) native support Unicode Standard, source file format is utf-8, variable can also be declared non-ASCII, for example, can be Chinese.


7) Tool Chain
Run one or more files directly without saving the compilation results.
$ go Run helloworld.go


Compile and run one or more files to save the compiled results.
$ go Build helloworld.go


Get the code for another warehouse
$ go Get Gopl.io/ch1/helloworld
You need to pre-follow the versioning tools, such as Git, and set the GOPATH environment variable, and the downloaded code will be placed in the $GOPATH/src/gopl.io/ch1/helloworld directory


GOFMT Automatic Formatting
Many text editors can be set to automatically execute GOFMT when a file is saved, so your source code should always be formatted.


Goimports
Goimports will automatically add the import declarations you need to use in your code and the import declarations that you need to remove. This tool is not included in the standard distribution package, but you can install it yourself:
$ go Get golang.org/x/tools/cmd/goimports




1.2
1) OS. The first element of args, the OS. ARGS[0] is the command line execution, and the other element is the argument that is passed to the program when the command is executed.


2) Slice s[m:n], left closed right open, M and n can be omitted, corresponding substitute for 0 or Len (s)


3) Notes
Single-line comments are represented by//.
Multiline comments are expressed as */*/.


4) + + 、--only supports post, and is a statement, not an expression


5) The VAR keyword is used to declare variables. Can be initialized when declared, or, if not, initialized to a 0 value of the corresponding type.
Use the: = Symbol to initialize and assign I, which is a shorthand form of Var xxx=yyy that can only be used in functions


6) Four ways to type variables
S: = ""
var s string
var s = ""
var s string = ""
So how do these equivalent forms be chosen? Here are a few suggestions:
The first form can only be used within a function, and package-level variables should not do so.
The second form relies on the internal initialization mechanism of the string type, which is initialized to an empty string.
The third form is rarely used unless multiple variables are declared at the same time.
The fourth form explicitly indicates the type of the variable, which can be used when multiple variables are declared at the same time.
In practice, you should use only the first two forms of the above, explicitly specifying the type of the variable, letting the compiler initialize its value yourself, or implicitly initializing it to indicate how the initial value does not matter.


7) Special Variable underline _, indicating that this value is ignored


8) for several forms
Normal
for initialization; Condition Post {
Zero or more statements
}


A traditional "while" loop
For condition {
// ...
}


A traditional infinite loop
for {
// ...
}


Range
For k,v: = Range os. Args[1:] {
}


In an infinite loop, you can still stop the loop by either a break or a return statement.
Continue Skip Current loop




1.3
1) Map is the Key/value type data structure built into the go language. Key supports any data type, as long as the type can be compared using the = = operator. When a range loop is performed on a map, its iteration order is indeterminate.


2) fmt. printf Common parameters
%d int variable
%x,%o,%b are 16 binary, 8 binary, 2 binary form int
%f,%g,%e floating point: 3.141593 3.141592653589793 3.141593e+00
%t Boolean variable: TRUE or False
%c Rune (Unicode code point), the Unicode character type that is unique in the Go language
%s string
%q string "abc" with double quotation marks or rune ' C ' with single quotation marks
%V will print any variable in an easy-to-read form
%T Types of print variables
Percent sign (% symbol itself, no other action)


3) functions and package-level variables can be declared in any order, without affecting their invocation.


4) IO operation
In some of the lower levels, Bufio. Scanner,ioutil. Both ReadFile and Ioutil.writefile use *os. The Read and write methods of file, but generally programmers do not need to be directly aware of their underlying implementation details, the methods provided in the Bufio and io/ioutil packages are good enough.




1.4
1) When we import a package with a packet path containing multiple words, such as image/color (image and color two words), usually we just need to use the last word to represent the package, such as color.


1.6
1) goroutine
Goroutine is a method of concurrent execution of a function, and channel is used to pass parameters between Goroutine.
The main function is also running in a goroutine, while the Go function means creating a new goroutine and executing the function in this new goroutine.


2) Channel
When a goroutine tries to do a send or receive operation on a channel, the goroutine blocks at the call until another goroutine writes to or receives a value in the channel.



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.