go_study/flysnow/ch04/main.go

60 lines
1.3 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package main
import (
"fmt"
"unicode/utf8"
)
func main() {
array := []string{"a", "b", "c", "d", "e"}
fmt.Println(array)
for i := 0; i < len(array); i++ {
fmt.Printf("数组索引: %d对应值: %s\n", i, array[i])
}
for i, v := range array {
fmt.Printf("数组索引: %d, 对应值: %s\n", i, v)
}
for _, v := range array {
fmt.Printf("数组值: %s\n", v)
}
// 切片slice
slice := array[2:4] // 包含索引2不包含索引4
fmt.Println(slice)
slice1 := make([]string, 4)
slice2 := make([]string, 4, 8)
slice2 = append(slice1, "f")
fmt.Println(slice2)
// 映射map
nameAgeMap := make(map[string]int)
nameAgeMap["Snowfay"] = 20
fmt.Println(nameAgeMap)
age := nameAgeMap["Snowfay"]
fmt.Println(age)
age, ok := nameAgeMap["Snowfay"]
if ok {
fmt.Println(age)
} else {
fmt.Println(ok)
}
nameAgeMap["飞雪无情"] = 50
fmt.Println(nameAgeMap)
delete(nameAgeMap, "飞雪无情")
fmt.Println(nameAgeMap)
nameAgeMap["Hello"] = 22
nameAgeMap["Allen"] = 33
for k, v := range nameAgeMap {
fmt.Println("Key is", k, "Value is", v)
}
fmt.Println(len(nameAgeMap))
// string
s := "Hello飞雪无情"
bs := []byte(s)
fmt.Println(bs)
fmt.Println(s[0], s[1], s[15])
fmt.Println(len(s))
fmt.Println(utf8.RuneCountInString(s))
for i, r := range s {
fmt.Printf("%d %c\n", i, r)
}
}