golang go go go!

本文最后更新于:25 天前

基础语法

变量声明

var hello string = "Hello, Golang"
hello2 := "Hello, Golang2"

import

import (
  "fmt"
  nickname "packageName" // 别名
  . "packageName" // 全部引入
)

首字母大写,可给其他包使用,小写不可以

数据类型

整型

关键字 范围
int8 -127 ~ 128
uint8(正数) 0 ~ 255
以此类推 以此类推

浮点型

关键字 备注
float32
float64 通常使用这个

字符串

双引号包裹

布尔值

true / false

other

查看数据类型

var a string = "hello, Golang"
fmt.Printf("%T", a) // 查看数据类型 输出string

类型转换

var c string = "123"
int, err := strconv.Atoi(c)
fmt.Println(int, err)

// api...还很多

流程控制

// 仅最常用
for i:= 0; i < 10; i++ {
  fmt.Println(i)
}

a := 10
for a > 1 {
  a--
  if a == 5 {
    continue
  }
  fmt.Println(a)
}

// 死循环
for {
  fmt.Println("hello")
}

数组

arr := [3]int{0, 1, 2}
arr2 := [...]int{0, 1, 2, 5, 6}

var arr3 = new([10]int)
arr3[5] = 8
fmt.Println(arr, arr2, arr3)
// [0 1 2] [0 1 2 5 6] &[0 0 0 0 0 8 0 0 0 0]
arr := [3]int{0, 1, 2}
arr2 := [...]int{0, 1, 2, 5, 6}

var arr3 = new([10]int)
arr3[5] = 8
fmt.Println(arr, arr2, arr3)

for i := 0; i < len(arr2); i++ {
	fmt.Print(arr2[i], " ")
}
fmt.Println()

zoom := [...]string{"猫咪", "狗子", "猴子", "狮子", "大熊猫"}
// forEach
for index, value := range(zoom) {
	fmt.Println(index, value)
}

// 二维数组
arrArr := [3][3]int {
	{0, 1, 2},
	{3, 4, 5},
	{6, 7, 8},
}
fmt.Println(arrArr)

map

// 三种声明方式

var m map[string]string
m = map[string]string{}
m["name"] = "qiuqian"
m["gender"] = "male"
fmt.Println(m)

m1 := map[int]bool{}
m1[1] = true
m1[2] = false
fmt.Println(m1)

m2 := make(map[int]interface{})
m2[1] = "qiuqian"
m2[2] = 20
m2[3] = []int{1, 2, 3}
fmt.Println(m2, len(m2))
delete(m2, 3)
fmt.Println(m2, len(m2))

for key, value := range m2 {
  fmt.Println(key, value)
}

for key := range m {
  fmt.Println(key, m[key])
}

func

package main

import "fmt"

func main() {

	// 延迟函数 defer关键字 最后执行
	defer emo()

	// 闭包函数 以后用到再说

	// 自执行函数
	(func() {
		fmt.Println("自执行函数, sei也别管我")
	})()

	res1, res2 := test(1, "hello")
	fmt.Println(res1, res2)

	res3, res4 := test2(1, 2)
	fmt.Println(res3, res4)

	anonymousfunc := func(date1 interface{}) {
		fmt.Println("我是匿名函数,下面是传入的值")
		fmt.Println(date1)
	}
	anonymousfunc("嗷呜")

	test3(0, "qiuqian", "aoowu", "ysml")

}

func test(date1 int, date2 string) (ret1 int, ret2 string) {
	fmt.Println(date1, date2)
	return date1 + 1, "return date"
}

func test2(date1 int, date2 int) (ret1 int, ret2 int) {
	ret1 = date1 + 1
	ret2 = date2 + 1
	// 以赋值,不用写
	return
}

// 不定项参数 以map存储
func test3(date1 int, date2 ...string) {
	fmt.Println("打印所有参数")
	fmt.Println(date1)
	for date := range date2 {
		fmt.Println(date2[date])
	}
}

func emo() {
	fmt.Println("延迟函数")
}

结构体

package main

import "fmt"

type Person struct {
	Name string
	Age int
	Gender bool
	Hobby []string
}

func main() {

	// 显示声明
	var qiuqian Person
	qiuqian.Name = "qiuqian"
	qiuqian.Age = 20
	qiuqian.Gender = true
	qiuqian.Hobby = []string{"code", "code2"}
	//fmt.Println(qiuqian)

	// 隐式声明
	//qiuqian2 := Person{
	//	Name: "qiuqian2",
	//	Age: 21,
	//	Gender: true,
	//	Hobby: []string{"hello", "golang"},
	//}
	//fmt.Println(qiuqian2)

	// 直接 new, 得到的是结构体的地址
	qiuqian3 := new(Person)
	qiuqian3.Name = "qiuqian3"
	//fmt.Println(qiuqian3)

	qiuqianFunc(qiuqian)

}

func qiuqianFunc(qiuqian Person) {
	fmt.Println(qiuqian)
}

结构体指针

package main

import "fmt"

type Person struct {
	Name string
	Age int
	Gender bool
	Hobby []string
}

func main() {
	qq := Person{
		Name:   "qiuqian",
		Age:    20,
		Gender: true,
		Hobby:  []string{"code", "travel", "sing"},
	}

	var qiuqianP *Person
	qiuqianP = &qq

	qiuqianP.Name = "qiuqian edited!"
	// . 的优先级更高,想要使用添加括号提高优先级
	(*qiuqianP).Name = "qiuqian edited again!"

	fmt.Println(qq, qiuqianP)

}

结构体方法

package main

import "fmt"

type Person struct {
	Name string
	Age int
	Gender bool
	Hobby []string
}

func (p *Person)write_code(name string)(retstr string) {
	retstr =  name + " is writing code.."
	return retstr
}


func main() {
	qq := Person{
		Name:   "qiuqian",
		Age:    20,
		Gender: true,
		Hobby:  []string{"code", "travel", "sing"},
	}

	res := qq.write_code(qq.Name)
	fmt.Println(res)

}

结构体和 JSON 有点类似,可以在 Object 里面嵌套 Object,结构体里面嵌套结构体。

接口和Interface

package main

import "fmt"

type Animal interface {
	Eat()
	Run()
}

type Cat struct {
	Name   string
	Gender bool
}
type Dog struct {
	Name string
}

func (d Dog) Run() {
	fmt.Println(d.Name, "开始跑...")
}

func (d Dog) Eat() {
	fmt.Println(d.Name, "开始吃...")
}

func (c Cat) Run() {
	fmt.Println(c.Name, "开始跑...")
}

func (c Cat) Eat() {
	fmt.Println(c.Name, "开始吃...")
}

var L Animal

func main() {
	c := Cat{
		Name: "Tom",
		Gender: true,
	}
	MyFunc(c)
	L.Run()
}

func MyFunc(a Animal) {
	L = a
}

goroutine 和 channel

以后用到再说

package main

import (
	"fmt"
	"sync"
)

func main() {

	var wg sync.WaitGroup
	wg.Add(1)

	// 和主线程无关
	go Run(&wg)
	wg.Wait()
}

func Run(wg *sync.WaitGroup) {
	fmt.Println("Run() running...")
	wg.Done()
}

本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!