You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
golang-tutorial/demo/point/dmoe002.go

48 lines
1.5 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package main
import (
"fmt"
"reflect"
)
// import (
// "fmt"
// ) go_basic_courses
func main() {
// 声明一个集合字典,默认为空
a := 15
fmt.Println("address of b is ", a)
fmt.Println("address of b is ", &a)
m := map[string]int{}
fmt.Println("address of b i is ", m)
fmt.Printf("Type of a is %T\n", m)
fmt.Printf("m值是 %v \n", m == nil)
var dir map[string]string
fmt.Printf("dir字典的值是: | %v \n", dir)
fmt.Printf("dir字典是否为nil | %v \n", dir == nil)
// nil map不能赋值,如果直接赋值会报错“panic: assignment to entry in nil map”下面语句将会报错
// dir["name"] = "Tinywan"
fmt.Println("----------------------------")
// 使用make函数进行初始化创建一个非nil的map
dir = make(map[string]string)
// map是引用类型未初始化的是指向nil初始化了以后应该就有自己的内存空间了所以不是nil
fmt.Printf("dir字典的值是: | %v \n", dir)
fmt.Printf("dir字典是否为nil | %v \n", dir == nil)
fmt.Println("----------------------------")
// make之后分配内存了,一旦分配了内存地址就不为空了,可以赋值了
dir["name"] = "Tinywan"
fmt.Printf("dir字典的键:值 |%v \n", dir)
fmt.Printf("dir字典的类型 |%v \n", reflect.TypeOf(dir))
fmt.Printf("name的值是 |%v \n", dir["name"])
// value, ok := s["ok"]
// if ok == nil {
// s := make(map[string]int)
// s["ok"] = 19
// fmt.Println(s["ok"])
// }
// fmt.Println(s["ok"])
}