内容覆盖:变量、判断、循环、函数、数组、指针、结构体、类型转换、接口类、并行
package main
import (
"fmt"
"time"
)
var ( //第四种, 这种因式分解关键字的写法一般用于声明全局变量
k int
l bool
)
// 第五种,常量
const q1, q2, q3 = 1, true, "str"
//九, 函数接口和多态
type Phone interface{
// 接口类 phone
call()
}
type ApplePhone struct{
}
type MiPhone struct{
}
func (apple ApplePhone) call(){
println("I'm ApplePhone, calling you")
}
func (mi MiPhone) call(){
println("I'm MiPhone, calling you")
}
func main(){
fmt.Println("Hellow")
var str string = "xx"+"yy"
fmt.Println( str )
// 一, 变量
// 第一种,指定变量类型,如果没有初始化,则变量默认为零值。
var a, b int = 1, 2
var c int
fmt.Println(a+b, c)
// 第二种,根据值自行判定变量类型。
var d = true
fmt.Println(d)
// 第三种, 直接声明+赋值
f := "test"
f2 := 23.456
fmt.Println(f, f2)
// 第四种
fmt.Println(k,l)
// 第五种
fmt.Println(q1, q2, q3)
println(len(q3))
println("--------------------------------")
println()
println()
// 二, 判断
if a < 20{
println("a<20 if ")
}else{
println("a<20 else")
}
var grade = "B"
marks := 90
switch marks {
case 90: grade = "A"
case 80: grade = "B"
case 50,60,70 : grade = "C"
default: grade = "D"
}
switch {
case grade == "A" :
fmt.Printf("优秀!\n" )
case grade == "B", grade == "C" :
fmt.Printf("良好\n" )
case grade == "D" :
fmt.Printf("及格\n" )
case grade == "F":
fmt.Printf("不及格\n" )
default:
fmt.Printf("差\n" );
}
println("--------------------------------")
println()
println()
// 三, 循环
for i := 0; i < 5; i++ {
println(i)
}
sum := 7
for ; sum<10; {
println(sum)
sum +=2
}
for true {
sum += 2
if sum > 20{
println(sum)
break
}
}
// 数组
stirngList := []string{"Tom", "Lily", "Wang", "Kim"}
for idx, item := range stirngList{
println(idx, item)
}
// map
map1 := make(map[int]float32)
map1[1] = 1.2
map1[2] = 1.5
for key, value := range map1{
println(key, value)
}
println("--------------------------------")
println()
println()
// 四, 函数
x1 := "ss"
y1 := "xx"
x1, y1 = swap(x1, y1)
println(y1)
println("--------------------------------")
println()
println()
// 五, 数组
arrayTest := []int{1, 2}
println(len(arrayTest))
arrayTest = append(arrayTest, 3)
println(len(arrayTest))
println(arrayTest[0],arrayTest[1],arrayTest[2])
matrix := [][]int{}
row1 := []int{1,2,3}
row2 := []int{4,5,6,7}
matrix = append(matrix, row1)
matrix = append(matrix, row2)
println(matrix[0])
fmt.Println(matrix[0])
fmt.Println(matrix)
println("--------------------------------")
println()
println()
// 六, 指针
pp := 10
var ip *int
println(&pp)
ip = &pp
println(ip)
ip_ := &pp
println(ip_)
println(*ip)
println("--------------------------------")
println()
println()
// 七, 结构体
type Books struct{
title string
book_id int
price float32
}
book1 := Books{title: "Go语言", book_id: 001, price: 123.50}
book2 := Books{"C++语言", 002, 10.50}
book3 := Books{title: "Python语言", price: 23.50}
fmt.Println(book1)
fmt.Println(book2)
fmt.Println(book3)
shop := []Books{book1, book2, book3}
fmt.Printf(shop[0].title)
println("--------------------------------")
println()
println()
// 八, 类型转换
xxx := 1
yyy := 2
println(xxx/yyy)
println(float32(xxx)/float32(yyy))
println("--------------------------------")
println()
println()
//九, 函数接口和多态
var myPhone Phone
myPhone = new(ApplePhone)
myPhone.call()
myPhone2 := new(MiPhone)
myPhone2.call()
println("--------------------------------")
println()
println()
// 十, 并行
// Go 语言支持并发,我们只需要通过 go 关键字来开启 goroutine 即可。
// 需要 import "time"
say("hello")
say("world")
println()
// 使用并行,如果主函数结束了,并行也会被杀死,所以需要sleep等待才能看出效果
go say("hello")
say("world")
}
func swap(x string, y string) (string, string){
return y, x
}
func say(str string){
for i:=0; i<5; i++{
time.Sleep(100 * time.Millisecond)
println(str)
}
}
并行通道
package main
import "fmt"
func sum(s []int, c chan int) {
sum := 0
for _, v := range s {
sum += v
}
c <- sum // 把 sum 发送到通道 c
}
func main() {
s := []int{7, 2, 8, -9, 4, 0}
c := make(chan int)
go sum(s[:len(s)/2], c)
go sum(s[len(s)/2:], c)
x, y := <-c, <-c // 从通道 c 中接收
fmt.Println(x, y, x+y)
}