[Journey with golang] 1. Basic knowledge

This chapter describes some of the basics golang. golang with c ++ to write very different, in my opinion, it is more like a mix of c ++ / java / python, and then after things after a series of additions and deletions.

golang biggest feature is simple, only 25 keywords; though it is a static language, but supports dynamic run-time type; it is not only a strongly typed language that supports implicit type inference. golang generics are not supported, but support reflex. golang comes gc, and native support for coroutines.

1 package main
2 
3 import "fmt"
4 
5 func main() {
6     fmt.Println("Hello, world!")
7 }

The first line defines a package called main, it is the package name of the executable program. All the code go head must have a package declaration, go to manage namespaces through package.

The third line import external reference to a package fmt, which is the standard IO package. Standard Library can be referenced by import, or even a custom third-party packages.

Meaning the other lines are very obviously.

go Source feature is very clear: to .go suffix; the default UTF8 encoding; ignore the semicolon; functions that begin with func, and {with peers; bag where the main function name must be a main.

go basic data types a slight change compared with c ++, it is worth noting that the characters are not char, but the rune.

bool not interchangeable with int.

Different types of int must be cast.

Floating point literals are automatically inferred float64.

Strings and string java, as are the constants. But it may be operated by the slice. The tail does not contain null characters. Slicing sub-string string string returned is still not slice.

go support pointers. Structure pointer still use to access the structure fields instead of ->. golang No -> the operator.

golang slices maintains three elements: a pointer points to the underlying array, the number of slices and the underlying array element capacity. You can create a slice through the array and built-in functions make.

map can be created by a literal, you can create by make functions. golang built-in map is not concurrent safe and can be solved by using a standard packet sync in the map. An element map value can not be modified individually, can only be solved by the whole replacement value.

The struct type may be any type; struct storage space is continuous, its fields stored in the order of declaration, there are required alignment between fields.

Golang code sequence is not necessarily the final executable program order of instructions compiled.

golang only for circulation.

Other content basis for comparison, not repeat them.

Guess you like

Origin www.cnblogs.com/JHSeng/p/12128461.html