This is a creation in Article, where the information may have evolved or changed.
Golang Study Notes
Basic syntax
Keywords (key word) && variables
- The
-
Keyword
Golang has only 25 keywords, as follows:
import type return const if else for range break select default< /span> goto switch fallthrough func interface case chan continue defer go map struct package
Variable declaration && Initialization && Assignment
The Golang variable declaration way differs from the C + + system language Obviously, to the Golang this kind of declaration way has discussed why the Go language put the type behind?
Golang introduces the var
keyword, and type information is placed after the variable name.
"Go
var n int//define an int variable
var f float32
var str string//define a string variable
var arr [10]int//array
var m map[int]int//Map
var p *int//pointer
var n int = 10//define and initialize
var n = 10
N: = 10//Effect sample, can be automatically pushed to n type
n = 100//Assignment
N2: = N//define and initialize
......
由上面示例可以看出,golang语句不需要分号作结束符,而且定义一个变量是被初始化的一般默认值是`0`,而指针是`nil`,`string`是`""`。
Package Main
Import "FMT"
Func Main () {
var n int
Fmt. PRINTLN ("int:", N)
var f float32
Fmt. Println ("float:", f)
var str string
Fmt. Println ("string:", str)
var arr [10]int
Fmt. Printf ("array:")
For I: = 0; I < Len (arr); i++ {
Fmt. Printf ("%d", Arr[i])
}
Fmt. Println ()
var p int
Fmt. PRINTLN ("int:", p)
}
/
Output
int:0
float:0
String
array:0 0 0 0 0 0 0 0 0 0
Int:
*/
```
Types (Type)
Built-in base type in Golang
- Boolean
- int8, Byte, int16, int, uint, uintptr
- float32, float64
- Complex64, complex128
- String
- Rune
- Error
Composite type
- Arrays (Array)
- Dictionary (map)
- Slicing (slice)
- struct (struct)
- Channel (Chan)
- Interface (interface)
- Pointer (pointer)
- References (Reference)
Process controls (flow control)
- Conditional statements
If Else
- SELECT statement
Switch Case Select
- Looping statements
For range
- Jump statement
Goto
Functions (function) && packages (package)
- function definition
"Go
Func Func_name () {
}
```
- Function call
- Indeterminate parameters
- Multiple return values
- Anonymous functions && Closures
- Package Management
Object oriented
- Methods (method)
- Anonymous fields
- Interface (interface)