1.简式的变量申明只能在函数内部
package main
myvar := 1 //non-declaration statement outside function body
func main() {
}
package main
func main() {
//这样才行 var x interface{}= nil
var x = nil //use of untyped nil
_ = x
}
package main
func main() {
// 先声明map
var m1 map[string]string //panic: assignment to entry in nil map
// 再使用make函数创建一个非nil的map,nil map不能赋值
//m1 = make(map[string]string)
// 最后给已声明的map赋值
m1["a"] = "aa"
m1["b"] = "bb"
}
package main
import "fmt"
func main() {
s := "test"
//字符串的索引操作返回byte值而非字符
fmt.Println(s[1])//101
sbytes := []byte(s)
sbytes[0] = 'y'
fmt.Println(string(sbytes))
}
package main
import (
"fmt"
"unicode/utf8"
)
func main() {
s := "♥"
fmt.Println(len(s)) //3
fmt.Println(utf8.RuneCountInString(s)) //1
}
package main
func main() {
x := []int{
1,
2 //syntax error: need trailing comma before newline in composite literal
}
_ = x
}
package main
import "fmt"
func main() {
var data *byte
var in interface{}
fmt.Println(data,data == nil) //prints: <nil> true
fmt.Println(in,in == nil) //prints: <nil> true
in = data
fmt.Println(in,in == nil) //prints: <nil> false
//'data' is 'nil', but 'in' is not 'nil'
}