在math包下,有int、float、unit,直接使用即可
支持使用指针直接访问内存空间
func main() {
a:=1
aPtr:=&a
fmt.Println(a,aPtr)
//%T获取变量类型
fmt.Printf("%T %T",a,aPtr )
}
######################################
1 0xc000128058
int *int
Process finished with the exit code 0
func main() {
var s string
fmt.Println("*"+s+"*")
fmt.Println(len(s))
}
######################################
**
0
//map[key type]value type
map1:=map[string]int{"one":1,"two":2}
map2:=map[string]int{}
map1["one"]=11
//make(type,capacity),map跟切片都是自增长度的存储,如果容量不够,就会分配新的内存空间然后把原来的值进行拷贝。如果我们提前就知道了map的容量,那么就可以使用map初始化,避免因为频繁扩容带来的开销,提高性能
map3:=make(map[string]int,10)
fmt.Println(map1)
fmt.Println(map2)
fmt.Println(map3)
func TestRangeMap(t *testing.T){
map1:=map[string]int{"one":1,"two":2,"three":3}
for k,v:=range map1{
t.Log(k,v)
}
}
func TestMap(t *testing.T){
map1:=map[string]int{"one":1,"two":2}
if v,ok:=map1["three"];ok{
fmt.Println(v)
}else{
fmt.Println("three not exist~")
}
}
######################################
=== RUN TestMap
three not exist~
map[]
map[]
--- PASS: TestMap (0.00s)
PASS
func TestFunValueMap(t *testing.T){
funcMap:=map[int]func(v int)int{}
funcMap[1]=func(v int)int{return v}
funcMap[2]=func(v int)int{return v*v}
funcMap[3]=func(v int)int{return v*v*v}
t.Log(funcMap[1](2),funcMap[2](2),funcMap[3](2))
}
######################################
=== RUN TestFunValueMap
operator_test.go:56: 2 4 8
--- PASS: TestFunValueMap (0.00s)
PASS
func TestSet(t *testing.T){
mySet:=map[int]bool{}
mySet[1]=true
n:=1
//判断元素是否存在
if mySet[n] {
t.Logf("%d is exist",n)
}else {
t.Logf("%d is not exist",n)
}
//查看set元素个数
t.Log(len(mySet))
//删除key
delete(mySet,1)
}
func TestChar(t *testing.T){
s:="中"
//byte数
t.Log(len(s))
c:=[]rune(s)
t.Logf("中 unicode %x",c[0])
t.Logf("中 UTF8 %x",s)
}
######################################
=== RUN TestChar
operator_test.go:78: 3
operator_test.go:80: 中 unicode 4e2d
operator_test.go:81: 中 UTF8 e4b8ad
--- PASS: TestChar (0.00s)
PASS
1&^0 --1
1&^1 --0
0&^1 --0
0&^0 --0
func TestOperator(t *testing.T){
a:=[...]int{1,2,3,5}
b:=[...]int{1,2,3,4}
c:=[...]int{1,2,3,5}
t.Log(a==b)
t.Log(a==c)
}
######################################
=== RUN TestOperator
operator_test.go:9: false
operator_test.go:10: true
--- PASS: TestOperator (0.00s)
PASS
func TestFor(t *testing.T) {
n := 0
//while n<5
for n < 5 {
t.Log(n)
n++
}
}
######################################
=== RUN TestFor
operator_test.go:17: 0
operator_test.go:17: 1
operator_test.go:17: 2
operator_test.go:17: 3
operator_test.go:17: 4
--- PASS: TestFor (0.00s)
PASS
func TestIf(t *testing.T) {
if v, err := someFunc(); err == nil {
t.Log("no err,value:", v)
} else {
t.Log("err:", err)
}
}
func TestIf(t *testing.T) {
if v, err := someFunc(); err == nil {
t.Log("no err,value:", v)
} else {
t.Log("err:", err)
}
}