Basic types
基本类型
bool
string
int int8 int16 int32 int64
uint uint8 uint16 uint32 uint64 uintptr
byte // alias for uint8
rune // alias for int32
// represents a Unicode code point
float32 float64
complex64 complex128
package main
import (
"math/cmplx"
"fmt"
)
var (
ToBe bool = false
MaxInt uint64 = 1<<64 - 1
z complex128 = cmplx.Sqrt(-5+12i)
)
func main() {
const f = "%T(%v)\n"
fmt.Printf(f, ToBe, ToBe)
fmt.Printf(f, MaxInt, MaxInt)
fmt.Printf(f, z, z)
}
输出:
bool(false)
uint64(18446744073709551615)
complex128((2+3i))
%T 是类型Type(%v)是值Value
Structs
结构体
A struct is a collection of fields.
结构体是很多字段的集合
package main
import "fmt"
type Vertex struct {
X int
Y int
}
func main() {
fmt.Println(Vertex{1, 2})
}
输出:
{1 2}
Struct Fields
结构体字段
Struct fields are accessed using a dot.
结构体字段用点.访问
package main
import "fmt"
type Vertex struct {
X int
Y int
}
func main() {
v := Vertex{1, 2}
v.X = 4
fmt.Println(v.X)
}
输出:
4
Pointers
Go has pointers, but no pointer arithmetic.
Go有指针,但是没有指针运算
Struct fields can be accessed through a struct pointer. The indirection through the pointer is transparent.
结构体字段能够通过结构体指针访问,很容易通过指针间接访问
package main
import "fmt"
type Vertex struct {
X int
Y int
}
func main() {
p := Vertex{1, 2}
q := &p
q.X = 1e9
fmt.Println(p)
}
输出:
{1000000000 2}
Struct Literals
结构体值
A struct literal denotes a newly allocated struct value by listing the values of its fields.
结构体值表示一个新分配结构体值通过列出它所有的字段值
You can list just a subset of fields by using the Name: syntax. (And the order of named fields is irrelevant.)
你也可以只通过名字语法勒出一部分字段值,顺序无所谓
The special prefix & constructs a pointer to a struct literal.
指定前缀&构造一个结构体字面值的指针
package main
import "fmt"
type Vertex struct {
X, Y int
}
var (
p = Vertex{1, 2} // has type Vertex
q = &Vertex{1, 2} // has type *Vertex
r = Vertex{X: 1} // Y:0 is implicit
s = Vertex{} // X:0 and Y:0
)
func main() {
fmt.Println(p, q, r, s)
}
输出:
{1 2} &{1 2} {1 0} {0 0}
The new function
new函数
The expression new(T) allocates a zeroed T value and returns a pointer to it.
表达式 new(T)返回一个分配了0默认值的T类型的指针
var t *T = new(T)
or
t := new(T)
package main
import "fmt"
type Vertex struct {
X, Y int
}
func main() {
v := new(Vertex)
fmt.Println(v)
v.X, v.Y = 11, 9
fmt.Println(v)
}
输出:
&{0 0}
&{11 9}
Maps
A map maps keys to values.
map匹配键和值
Maps must be created with make (not new) before use; the nil map is empty and cannot be assigned to.
Maps用之前必须用make创建,没有make的map是空的不能赋值
package main
import "fmt"
type Vertex struct {
Lat, Long float64
}
var m map[string]Vertex
func main() {
m = make(map[string]Vertex)
m["Bell Labs"] = Vertex{
40.68433, 74.39967,
}
fmt.Println(m["Bell Labs"])
}
输出:
{40.68433 74.39967}
Map literals are like struct literals, but the keys are required.
Map的字面值和结构体字面值一样,但是必须有键值
package main
import "fmt"
type Vertex struct {
Lat, Long float64
}
var m = map[string]Vertex{
"Bell Labs": Vertex{
40.68433, -74.39967,
},
"Google": Vertex{
37.42202, -122.08408,
},
}
func main() {
fmt.Println(m)
}
输出:
map[Google:{37.42202 -122.08408} Bell Labs:{40.68433 -74.39967}]
If the top-level type is just a type name, you can omit it from the elements of the literal.
如果顶级类型只是类型名的话,可以省略
package main
import "fmt"
type Vertex struct {
Lat, Long float64
}
var m = map[string]Vertex{
"Bell Labs": {40.68433, -74.39967},
"Google": {37.42202, -122.08408},
}
func main() {
fmt.Println(m)
}
输出:
map[Google:{37.42202 -122.08408} Bell Labs:{40.68433 -74.39967}]
Mutating Maps
Maps变型
Insert or update an element in map m:
插入或修改map m的一个元素
m[key] = elem
Retrieve an element:
提取一个元素
elem = m[key]
Delete an element:
删除一个元素
delete(m, key)
Test that a key is present with a two-value assignment:
测试键里是否有值
elem, ok = m[key]
If key is in m, ok is true. If not, ok is false and elem is the zero value for the map's element type.
如果键有对应值,ok 是true,如果没有,ok是false,并且对应map's元素类型elem是0值
Similarly, when reading from a map if the key is not present the result is the zero value for the map's element type.
相似的是当从map取值的时候如果键值不存在,返回结果也是map's元素类型默认值
package main
import "fmt"
func main() {
m := make(map[string]int)
m["Answer"] = 42
fmt.Println("The value:", m["Answer"])
m["Answer"] = 48
fmt.Println("The value:", m["Answer"])
delete(m, "Answer")
fmt.Println("The value:", m["Answer"])
v, ok := m["Answer"]
fmt.Println("The value:", v, "Present?", ok)
}
输出:
The value: 42
The value: 48
The value: 0
The value: 0 Present? false