2024-07-12
한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina
$GOPATH/pkg/mod
Down$GOPATH/bin
DownThe naming of go variables, constants, custom types, packages, and functions must follow the following rules:
The first character can be任意Unicode字符
or下划线
The part other than the first character can be a Unicode character, an underscore, or a number
No limit on the length of the name
In theory, a name can contain Chinese characters, or even be entirely Chinese characters, but in practice, don't do this.
break | default | func | interface | select |
---|---|---|---|---|
case | defer | go | map | struct |
chan | else | goto | package | switch |
const | if | range | type | continue |
for | import | return | fallthrough | var |
•constant
true
false
iota
nil
•type of data
int
int8
int16
int32
int64
uint
uint8
uint16
uint32
uint64
float32
float64
complex128
complex64
byte
rune
string
bool
error
uintptr
•function
make
len
cap
new
append
copy
close
delete
complex
real
imag
panic
recover
type | Go variable types | fmt output |
---|---|---|
Integer | int int8 int16 int32 int64 uint uint8 uint16 uint32 uint64 | %d |
Floating point | float32 float64 | %f %e %g |
plural | complex128 complex64 | %v |
Boolean | bool | %t |
pointer | uintptr | %d |
References | map slice channel | %v |
byte | byte | %d |
Any character | rune | %d |
String | string | %s |
mistake | error | %v |
If not explicitly initialized after declaration, numeric types are initialized to O, strings are initialized to an empty string, and Boolean types are initialized to false.
Reference types, functions, pointers, and interfaces are initialized to nil
Variables inside a function (non-global variables) can be declared and initialized using:
Underscores indicate anonymous variables
Anonymous variables do not occupy namespace and do not allocate memory, so they can be reused
Constants must be assigned values when they are defined, and their values cannot be changed during program execution.
const PI float32=3.14
const(
PI=3.14
E=2.71
)
const(
a=100
b //100,跟上一行的值相同
c //100,跟上一行的值相同
)
const(
a = iota //0
b //1
c //2
d //3
)
const(
a = iota //0
b //1
_ //2
d //3
)
const(
a = iota //0
b = 30 //30
c = iota //2
d //3
)
Literal: The value appears directly without the variable name. Literals of basic types are equivalent to constants.
Different data types cannot be compared, except for literals.
fmt.Printf("9tn", 04 ==4.00) //用到了整型字面量和浮点型字面量
fmt.Printf("%vn",.4i) //虚数字面量0.4i
fmt.Printf("%tn", "u4f17'=='众 //Unicode和rune字面量
fmt.Printf("Hello nWorldn!n") //字符串字面量
var (
A=3 //全局变量,大写字母开头,所有地方都可以访问,跨package访问时需要带上package名称
b=4 //全局变量,小写字母开头,本package内都可以访问
)
func fool{
b:=5 //局部变量,仅本函数内可以访问。内部声明的变量可以跟外部声明的变量有冲突,以内部的为准
{
b:=6 //仅小圈定的作用域内可以访问,可以跟外部的变量有冲突
}
}
Package comments. Above package xxx. A package only needs to have package comments in one place, usually a doc.go file.
There is only one line packagexxx and comments about the package
Struct comment. Above type xxx struct
Function comments. Above func xxx()
Line comment. Above or to the right of the line