This is a creation in Article, where the information may have evolved or changed.
//今天第一次看Golang,作为初学者总结一下
Why go?
Young, its birth must have his meaning, according to everyone's introduction to him, summed down as follows:
The go language is undoubtedly more efficient than most other languages in the field of high-performance distributed systems, and the go language is highly expressive, concise, clear and efficient. Thanks to its concurrency mechanism, programs written in it can make very efficient use of multicore and networked computers, and its novel type system makes the program structure flexible and modular. The go code compiles into machine code not only very quickly, but also has a handy garbage collection mechanism and a powerful runtime reflection mechanism. It is a fast, static type of compiled language.
The above is an introduction to him, then I am interested, I intend to study, learn to learn
//今天简单了解了基础语法
Basic composition
package mainimport "fmt"func main() { fmt.Println("hello")}
Can see
- Package Main Declaration Pack
- The import "FMT" tells the compiler that this program needs to use the FMT package, and the FMT package implements the format IO (input/output) function.
- The func main () is the beginning of the program. The main function is what each executable program must contain, typically the first function executed after startup (if there is an init () function, the function is executed first).
- Fmt. Println (...) You can output a string to the console and automatically increment the newline character at the end
\n
.
BASIC syntax structure
Row delimiter
Go is not the end of every statement like C ;
, and in the world of Go, a line is the end of a statement. Of course, if you want to write multiple statements on the same line, you must ;
separate them. The rest is given to the compiler to complete
Comments
Single-line comment to //
begin with, multiline comment with /* ... */
parcel
Identifier
The first character of the identifier must be a letter or an underscore. Not available keywords
Data type
- Number type. Support for shaping
int
and floating-point types, float32
float64
defined without specifying, compiler recognition.
- The string type. The string of Go is connected by a single byte. The byte of the Go language uses UTF-8 encoding to identify Unicode text.
- The Boolean type. The value of a Boolean can only be a constant true or false.
- The derived type. Includes pointer type (Pointer), array type, structured type (struct), Channel type, function type, tile type, interface type (interface), MAP type.
Variable declaration
Consists of letters, numbers, underscores, and cannot begin with a number.
The declaration generally uses var
keywords.
Specifies the variable type, with the default value if the value is not assigned after declaration.
var v_name v_typev_name = value
- The variable type is determined by the value itself
var v_name = value
.
- Omitted
var
, note that :=
the left-hand variable should not have been declared, otherwise it would result in a compilation error.
Multi-variable Declaration
var vname1, vname2, vname3 = v1, v2, v3 vname1, vname2, vname3 := v1, v2, v3
- There is a very interesting, declared a useless variable, compile error
Constant
const identifier [type] = value
Operator
Arithmetic operators
+ - * / % ++ --
Relational operators
== != > < >= <=
logical operators
&& || !
Bitwise operators
& | ^ << >>
Assignment operators
= += -= *= /= %= <<= >>= &= |= ^=
Special operators
& //&a 返回变量a的地址* //*a 是一个指针变量
Conditional statements
In addition if else switch
select
to the outside, and then alone to studyselect
Looping statements
for a := 0; a < 10; a++ { fmt.Printf("a 的值为: %d\n", a)}//像while一样var a, b int = 1, 3for a < b { a++ fmt.Printf("a 的值为: %d\n", a)}//也能这么用...有意思numbers := [6]int{1, 2, 3, 5}for i, x := range numbers { fmt.Printf("第 %d 位 x 的值 = %d\n", i, x)}//还有goto
//明天接着看