golang map简单应用
package main import "fmt" //如果用户名存在就将密码改为888888,不存在就新增该用户信息 func main() { user := make(map[string]map[string]string, 10) user["smith"] = make(map[string]string, 2) user["smith"]["password"] = "999999" user["smith"]["nickname"] = "史密斯" modifyUser(user, "tom") modifyUser(user, "mary") modifyUser(user, "smith") fmt.Println(user) } func modifyUser(user map[string]map[string]string, name string) { if user[name] != nil { //如果有该用户则只是修改密码 user[name]["password"] = "888888" } else { //没有该用户则新添该用户 user[name] = make(map[string]string, 2) user[name]["password"] = "888888" user[name]["nickname"] = name } }