切片与数组
type P struct {
name string
password int
}
func main() {
//p := [3]P{
// {"wqdi", 123},
// {"zhang", 123123},
// {"qina", 123123123},
//}
//
//for i := range p {
// fmt.Println(p[i])
//}
p := make([]P, 3)
p[0] = P{"zhang", 123}
p[1] = P{"sun", 123123}
p[2] = P{"wang", 123123123}
for _, i := range p {
fmt.Println(i)
}
}
map
import "fmt"
func main() {
//mp :=make([]string,3)
mp := make(map[string]int)
mp["one"] = 1
mp["two"] = 2
mp["three"] = 3
fmt.Println(len(mp))
//delete(mp, "one")
//fmt.Println(mp["one"])
r, ok := mp["one"]
fmt.Print(r, ok)
fmt.Println("===================")
for i, t := range mp {
fmt.Println(i, " ", t)
}
}
函数
import "fmt"
func add(a int, b int) int {
return a + b
}
func add1(a string, b string) string {
return a + b
}
func check(mp map[string]int, s string) (num int, f bool) {
num, f = mp[s]
return num, f
}
func main() {
fmt.Println(add(1, 2))
mp := make(map[string]int)
mp["qwd"] = 1
mp["qoif"] = 2
mp["oiqmwd"] = 3
val, ok := check(mp, "oiqmwd")
fmt.Println(val, " ", ok)
}
结构体函数
type P struct {
name string
password int
}
func (u *P) ch(name string, password int) (err error) {
u.password = password
u.name = name
return err
}
func main() {
p := make([]P, 5)
p[0] = P{"zhang", 123}
p[1] = P{"sun", 123123}
p[2] = P{"wang", 123123123}
p[3] = P{"yang", 123456}
for _, i := range p {
fmt.Println(i)
}
p[1].ch("AXTON", 347)
for _, i := range p {
fmt.Println(i)
}
}
标签:语言,int,fmt,语法,Println,mp,func,GO,string
From: https://www.cnblogs.com/Aidan347/p/17058142.html