This is a creation in Article, where the information may have evolved or changed.
After learning a language, suddenly stop for six months, it is easy to forget. and learn again!
So in order to quickly learn + review Golang, did this summary of finishing
First of all, Golang data collation
The Chinese version of the "Go"
Concept, import and visibility of 4.2.1 packages
你必须在源文件中非注释的第一行指明这个文件属于哪个包 如:package main。 package main表示一个可独立执行的程序,每个 Go 应用程序都包含一个名为 main 的包。包名只能是 小写字母如果对一个包进行更改或重新编译,所有引用了这个包的客户端程序都必须全部重新编译。
The wording of the import package
//method 1 import "fmt" import "os" //method 2 import "fmt";import "os" //method 3 import ("fmt"; "os") //method 4 import ( "fmt" "os" )
Visibility rules
当标识符(包括常量、变量、类型、函数名、结构字段等等)以一个大写字母开头, 如:Group1,那么使用这种形式的标识符的对象就可以被外部包的代码所使用(客户端程序需要先导入这个包),这被称为导出(像面向对象语言中的 public);标识符如果以小写字母开头,则对包外是不可见的,但是他们在整个包的内部是可见并且可用的(像面向对象语言中的 private )。
Group1 (initial capitalization, scope public)
group1 (lowercase first letter, scope private)
Precautions
If you import a package without using it, you will throw an error when building the program, such as imported and not Used:os, which follows the motto of Go: "No unnecessary code!" “。
Hierarchical declaration and initialization of packages
You can define or declare 0 or more constants (const), variables (Var), and types (type) after importing the package using import, and the scope of these objects is global (within the scope of this package), so you can be called by all functions in this package (such as Gotemplate.go C and V in the source file), and then declare one or more functions (func).
Variable definition
type IZ inttype ( IZ int FZ float64
4.2.2 function
The opening brace {must be placed on the same line as the declaration of the method, which is the compiler's mandatory, otherwise you will get an error when using GOFMT:
Build-error:syntax error:unexpected semicolon or newline before {
Although the Go language does not seem to use semicolons as the end of a statement, the process is actually done automatically by the compiler, so errors like the one above are thrown
Can also be written as a line
func Sum(a, b int) int { return a + b }
The usage rules for curly braces {} are the same at all times (for example, if statements, and so on).
Therefore, a function that conforms to the specification is generally written in the following form:
func functionName(parameter_list) (return_value_list) { …}
- Parameter_list in the form of (param1 type1, param2 type2, ...)
- Return_value_list in the form of (Ret1 type1, Ret2 type2, ...)
Use uppercase letters only when a function needs to be called by an external package (the public mentioned earlier), followed by the Pascal nomenclature, otherwise it follows the camel nomenclature, that is, the first letter in lowercase and the first letter of the remaining words capitalized.
下面这一行调用了 `fmt` 包中的 `Println` 函数,可以将字符串输出到控制台,并在最后自动增加换行字符 `\n`:```gofmt.Println("hello, workd")```使用 `fmt.Print("hello, world\n")` 可以得到相同的结果。
Notice Print does not automatically add \ n newline characters and you need to add them yourself.
4.2.3 Notes
Go text Format, default UTF8
Single-line comments are the most common form of annotation, and you can use a single-line comment at any point, starting with//. Multiline comments are also called block annotations, which begin with/* and end with a/* and cannot be nested, and multiline annotations are typically used for package document descriptions or comment blocks of code snippets.
The Godoc tool (Section 3.6) collects these comments and produces a technical document.
4.2.4 Type
Variables (or constants) that can contain data can use different data types or types to hold the data. The value of a variable declared with Var is automatically initialized to the 0 value of that type. A type defines a collection of values for a variable and a collection of actions that can be performed on it.
Types can be basic types, such as: int, float, bool, string, structured (composite), such as struct, array, slice, map, channel, and only describe the behavior of the type, such as: interface.
A structured type has no real value, it uses nil as the default value (nil in objective-c, NULL in Java, NULL in C and C + +, or 0). It is important to note that there is no type inheritance in the Go language.
A function can also be a deterministic type, that is, a function as the return type. This type of declaration is written after the function name and optional argument list, for example:
func FunctionName (a typea, b typeb) typeFunc
You can return a variable var using type Typefunc somewhere in the body of the function:
return var
A function can have multiple return values, and the return types need to be separated by commas and ()
enclosed in parentheses, such as:
func FunctionName (a typea, b typeb) (t1 type1, t2 type2)
Example: function Atoi (section 4.7):func Atoi(s string) (i int, err error)
Returned in the form:
return var1, var2
This multiple return value is typically used to determine whether a function succeeds (True/false) or returns an error message along with other return values, as described in parallel assignments.
With the type keyword you can define your own type (a typedef like C + +), you may want to define a struct (chapter 10th), but you can also define an alias for a type that already exists, such as:
type IZ int
This is not a true alias, because the type after which it is defined can have more attributes and must be explicitly converted when the type is converted.
Then we can declare the variable in the following way:
var a IZ = 5
Here we can see that int is the underlying type of variable a, which makes it possible to convert between them (4th. 2.6).
If you have more than one type to define, you can use the Factoring keyword, for example:
type ( IZ int FZ float64 STR string)
Each value must be compiled to belong to a type (the compiler must be able to infer the type of all values) because the Go language is a static type language.