目录
q := [...]int{1, 2, 3}
fmt.Printf("%T\n", q) // "[3]int"
a := [2]int{1, 2}
b := [...]int{1, 2}
c := [2]int{1, 3}
fmt.Println(a == b, a == c, b == c) // "true false false"
d := [3]int{1, 2}
fmt.Println(a == d) // compile error: cannot compare [2]int == [3]int
func main() {
a := [...]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 0}
s := a[:4]
s2 := s[:7]
fmt.Println(s, s2)//[1 2 3 4] [1 2 3 4 5 6 7]
fmt.Println(cap(s)) //10
fmt.Println(s[7]) //panic:index out of range
fmt.Printf("%T\n", s) //[]int
}
s1 := "hello,world"
s2 := s1[:5]
fmt.Println(cap(s1)) //panic
fmt.Println(len(s1)) //11
fmt.Println(cap(s2)) //panic
fmt.Println(len(s2)) //5
fmt.Printf("%T\n", s2) //string
s := []int{0, 1, 2, 3, 4, 5, 6, 7, 8}
make([]T, len)
make([]T, len, cap) // same as make([]T, cap)[:len]
var x []int
x = append(x, 1)
x = append(x, 2, 3)
x = append(x, 4, 5, 6)
x = append(x, x...) // append the slice x
fmt.Println(x)
// "[1 2 3 4 5 6 1 2 3 4 5 6]"
type IntSlice struct {
ptr
*int
len, cap int
}
data := []string{"hello", " ", ",", "world" }
data = nonempty(data)
copy(slice[i:], slice[i+1:])
var a1 []int //只是声明这么一个类型,对应的零值为nil
a2 := make([]int, 3) //这里创建了一个[]int数据,并且长度为3,每个元素对应零值
0
var b1 map[string]int //只是声明,对应零值为nil,不能添加值
b2 := make(map[string]int) //这里创建了map[string]int数据,并且可以添加值了
ages := make(map[string]int)
ages["alice"] = 31
ages["charlie"] = 34
delete(ages, "alice") // remove element ages["alice"]
age1 := ages["bob"] //如果存在,返回值;否则,返回相应的零值
age2, ok := ages["bob"] //如果存在,返回value, true;否则,返回零值,false
_ = &ages["bob"] // compile error: cannot take address of map element
func equal(x, y map[string]int) bool {
if len(x) != len(y) {
return false
}
for k, xv := range x {
if yv, ok := y[k]; !ok || yv != xv {
return false
}
}
return true
}
import "sort"
var names []string
for name := range ages {
names = append(names, name)
}
sort.Strings(names)
for _, name := range names {
fmt.Printf("%s\t%d\n", name, ages[name])
}
var graph = make(map[string]map[string]bool)
func addEdge(from, to string) {
edges := graph[from]
if edges == nil {
edges = make(map[string]bool)
graph[from] = edges
}
edges[to] = true
}
func hasEdge(from, to string) bool {
return graph[from][to]
}
对于空结构体struct{},它的大小为0,应该尽量避免
type Point struct{ X, Y int }
p := Point{1, 2}
p := Point{X: 1, Y: 2}
p2 := Point{
X; 1,
Y: 2,
}
package p
type T struct{ a, b int } // a and b are not exported
package q
import "p"
var _ = p.T{a: 1, b: 2} // compile error: can't reference a, b
var _ = p.T{1, 2}// compile error: can't reference a, b
type Point struct {
X, Y int
}
type Circle struct {
Center Point
Radius int
}
type Wheel struct {
Circle Circle
Spokes int
}
var w Wheel
w.Circle.Center.X = 8
w.Circle.Center.Y = 8
w.Circle.Radius = 5
w.Spokes = 20
以上代码的等价形式(注释部分写法依然有效)如下:
type Point struct {
X, Y int
}
type Circle struct {
Point
Radius int
}
type Wheel struct {
Circle
Spokes int
}
var w Wheel
w.X = 8 // equivalent to w.Circle.Point.X = 8
w.Y = 8 // equivalent to w.Circle.Point.Y = 8
w.Radius = 5 // equivalent to w.Circle.Radius = 5
w.Spokes = 20
w = Wheel{Circle{Point{8, 8}, 5}, 20}
w = Wheel{
Circle: Circle{
Point:
Point{X: 8, Y: 8},
Radius: 5,
},
Spokes: 20, // NOTE: trailing comma necessary here (and at Radius)
}
boolean true
number -273.15
string "She said \"Hello, BF\""
array ["gold", "silver", "bronze"]
object {"year": 1980,
"event": "archery",
"medals": ["gold", "silver", "bronze"]}
encoding/json的基本操作:
1.Marshal
2.Unmarshal
3.MarshalIndent
4.Decoder
5.Encoder
详细的在反射章节补充
原文:https://www.cnblogs.com/laiyuanjing/p/11227776.html