一、变量(variables.go)
package main
func main() {
//1.变量声明
var v1 int
var v2 string
var v3 [10]int
var v4 []int
var v5 map[string]int
var v6 struct {
f int
}
var v7 *int
var v8 func(a int) int
var (
v9 int
v10 [5]int
)
//2.变量初始化
var v11 int = 1
var v12 = 1
v13 := 1
//3.变量赋值
var v14 int
v14 = 1
//1>多重赋值
var i int = 1
var j int = 2
i, j = j, i
//4.匿名变量
_, _, nickName := GetName()
}
func GetName() (firstName, lastName, nickName string) {
return "May", "Chan", "Chibi Maruko"
}
二、常量(const.go)
package main
import "fmt"
//import "os"
func main() {
//1.字面常量
//2.常量定义
const PI float32 = 3.1415926
const P = 1
const (
AA int64 = 1024
BB int = 2
)
const A, B, C = 1, 2, "abc"
//const D = os.Getenv("HOME") // const initializer os.Getenv("HOME") is not a constant
fmt.Println(A, B, C)
//3.预定义常量
const (
A1 = iota
A2 = iota
A3 = iota
)
const (
B1 = iota
B2
B3
)
fmt.Println(A3, B3)
}
三、类型(type.go)
package main
import (
"fmt"
"math"
)
func main() {
//1.布尔类型
var a1 bool
a1 = true
a2 := (1 == 1)
var a3 bool
//a3 = 1 //cannot use 1 (type int) as type bool in assignment
fmt.Println(a1, a2, a3)
//2.整数类型
//1>类型表示
var b1 int32 //b1 declared and not used
b2 := 24
b1 = int32(b2) //cannot use b2 (type int) as type int32 in assignment
//2>数值运算 +、 、*、/和%
b1 = 5 % 2
fmt.Println(b1)
//3>比较运算 >、<、==、>=、<=和!=
//4>逻辑运算 x << y 左移 x >> y 右移 x^y 异或 x&y 与 x|y 或 ^x 取反
//3.浮点数
//1>浮点数表示
var c1 float32
c2 := 12.0
c1 = float32(c2) //cannot use c2 (type float64) as type float32 in assignment
fmt.Println(c1, c2)
//2>浮点数比较
var (
c3 float64 = 12.1
c4 float64 = 12.0
)
rs := isEqual(c3, c4, 0.001)
fmt.Println(rs)
//4.复数
//1>复数表示
var d1 complex64
d1 = 12 + 12i
d2 := 12 + 12i
d3 := complex(12, 12)
fmt.Println(d1, d2, d3)
//5.字符串
var str string
str = "Hello world"
ch := str[0]
fmt.Printf("The length of \"%s\" is %d \n", str, len(str))
fmt.Printf("The first character of \"%s\" is %c.\n", str, ch)
//1>字符串操作
var (
x string = "Hello"
y string = "123"
)
fmt.Printf("字符串连接\"%s\",字符串长度\"%s\",取字符\"%s\" \n", x+y, len(x), x[1])
//2>字符串遍历
n_str := "Hello,世界"
n := len(n_str)
for i := 0; i < n; i++ {
ch := n_str[i] // 的 为byte
fmt.Println(i, ch)
}
for i, ch := range n_str {
fmt.Println(i, ch) //ch的 为rune
}
//6.字符类型
var e1 byte
var e2 rune
fmt.Println(e1, e2)
//7.数组
//1>数组声明
var f1 [32]float32
var f2 [2 * 2]int
var f3 [10]*float64
var f4 [2][2]int
fmt.Println(f1, f2, f3, f4)
//2>数组遍历
for i := 0; i < len(f2); i++ {
fmt.Println("Element", i, "of array is", f2[i])
}
for i, v := range f2 {
fmt.Println("Array element[", i, "]=", v)
}
//3>值类型
f5 := [5]int{1, 2, 3, 4, 5} // annot use f5 (type [5]int) as type [10]int in argument to modify
modify(f5)
fmt.Println("In main(), array values:", f5)
//8.数组切片
//1>创建数组切片 基于数组或直接创建
var myArray [10]int = [10]int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
var mySlice []int = myArray[:5] // myArray[:]所有 myArray[5:]从第5个开始 myArray[:5]前5个
fmt.Println("Elements of myArray: ")
for _, v := range myArray {
fmt.Print(v, " ")
}
fmt.Println("\nElements of mySlice: ")
for _, v := range mySlice {
fmt.Print(v, " ")
}
//创建一个初始元素个数为5的数组切片,元素初始值为0:
mySlice1 := make([]int, 5)
//创建一个初始元素个数为5的数组切片,元素初始值为0,并预留10个元素的存储空间:
mySlice2 := make([]int, 5, 10)
//直接创建并初始化包含5个元素的数组切片:
mySlice3 := []int{1, 2, 3, 4, 5}
fmt.Println("\n", len(mySlice1), len(mySlice2), len(mySlice3))
//2>元素遍历
//3>动态增减元素
mySlice4 := make([]int, 5, 10)
mySlice4 = append(mySlice4, 1, 2, 3)
mySlice4 = append(mySlice4, mySlice1...)
fmt.Println("len(mySlice4):", len(mySlice4))
fmt.Println("cap(mySlice4):", cap(mySlice4))
//4>基于数组切片创建数组切片
oldSlice := []int{1, 2, 3, 4, 5}
newSlice := oldSlice[:3]
for _, v := range newSlice {
fmt.Println("前三个元素", v)
}
//5>内容复制
slice1 := []int{1, 2, 3, 4, 5}
slice2 := []int{5, 4, 3}
copy(slice2, slice1)
for _, v := range slice2 {
fmt.Println("slice2", v)
}
slice1 = []int{1, 2, 3, 4, 5}
slice2 = []int{5, 4, 3}
copy(slice1, slice2)
for _, v := range slice1 {
fmt.Println("slice1", v)
}
}
func modify(array [5]int) {
array[0] = 10
fmt.Println("In modify(), array values:", array)
}
func isEqual(f1, f2, p float64) bool {
return math.Dim(f1, f2) < p
}
四、Map(map.go)
package main
import "fmt"
type PersonInfo struct {
ID string
Name string
Address string
}
func main() {
//1.变量声明 personDB代表变量名 string代表键类型 PersonInfo代表值类型
var personDB map[string]PersonInfo
//2.创建,可指定存储能力
personDB = make(map[string]PersonInfo, 20)
//创建并初始化
personDB2 := map[string]PersonInfo{"1234": PersonInfo{"1234", "tom", "room"}}
//3.元素赋值
personDB["12345"] = PersonInfo{"12345", "Tom", "Room 203,..."}
personDB["1"] = PersonInfo{"1", "Jack", "Room 101,..."}
//4.元素删除
delete(personDB2, "1")
//5.元素查找
person, ok := personDB["1234"]
if ok {
fmt.Println("Found person", person.Name, "with ID 1234.")
} else {
fmt.Println("Did not find person with ID 1234.")
}
}
发表评论 取消回复