Golang学习笔记-Go语言基础

Go语言基础

代码虽然简单,但是还是需要自己手动去写一遍才知道其中的道理。

package main

import (
	"container/list"
	_ "container/list"
	"flag"
	_ "flag"
	"fmt"
	"time"
)

//使用flag从命令行中读取参数,不知道用来干嘛
func pack_flag_test(){
    
    
	surname := flag.String("surname","luo","您的姓")
	var personalName string
	flag.StringVar(&personalName,"personalName","xiaoer","您的名")
	id := flag.Int("id",0,"您的ID")
	//解析命令行参数
	flag.Parse()
	fmt.Printf("I am %v%v, and my id is %v\n",*surname,personalName,*id)
}

//指针操作
func pack_point_operate(){
    
    
	str := "Golang is good"
	strPrt := &str //取地址
	fmt.Printf("str type is %T,value is %v,address is %p\n", str, str, &strPrt)
	fmt.Printf("strPrt type is %T,value is %v\n", strPrt, strPrt)

	newStr := *strPrt  //解引用,拷贝内容,但是地址不是原来str的地址
	fmt.Printf("newStr type is %T,value is %v,address is %p\n", newStr, newStr, &newStr)
}

//变量命名别名
type aliasInt = int
//定义新的类型
type myInt int
func pack_alias_test(){
    
    
	var alias aliasInt
	fmt.Printf("alias value is %v, type is %T\n", alias, alias)

	var val myInt
	fmt.Printf("val value is %v, type is %T\n", val, val)
	/*
	* 结果:
	* alias value is 0, type is int
	* val value is 0, type is main.myInt
	*/
}

//if与switch
func if_switch_test(name string, age int){
    
    
	if age >= 0 {
    
    
		age += 1
	}else if age < 0 {
    
    
		age = 1
	}


	/* switch语句
	1,不需要使用 break
	2,穿透关键字 fallthrough
	3,可以使用字符串和数值等
	 */
	switch name {
    
    
	case "xiaoming":
		fmt.Printf("I am xiaoming\n")
		//不需要使用break
	case "DD":
		fmt.Printf("I am DD\n")
		//不需要使用break
	case "":
		fmt.Printf("NULL\n")
		fmt.Printf("from fallthrough\n")
		fallthrough
	default:
		fmt.Printf("to fallthrough\n")
	}

	var i int
	for i=0;i<=10;i++{
    
    
		fmt.Printf("i:%d\n",i)
	}
}

//常用的容器
/*
1,数组
2,切片
3,列表
4,字典
 */
func container_test(){
    
    
	//数组初始化的方法1
	var array [10] string
	array[0] = "这是第一个字符串"
	array[1] = "second"
	fmt.Printf("array:%v\n",array)

	//数组初始化的方法2
	array2 :=[...]string{
    
    "one","two","three"}
	fmt.Printf("array2:%v\n",array2)

	//数组初始化的方法3,指针操作数组
	array3 := new([3]string)
	array3[0] = "这是第一个字符串array3"
	array3[1] = "array3 second"
	//Println打印输出指针
	fmt.Println(*array3)

	//切片
	source := [...]int{
    
    1,2,3}
	sli := source[0:1]
	fmt.Printf("sli value is %v\n",sli)
	fmt.Printf("sli len is %v\n", len(sli))
	fmt.Printf("sli cap is %v\n",cap(sli))
	/*结果
	sli value is [1]
	sli len is 1
	sli cap is 3
	*/

	//动态创建切片
	sli2 := make([]int, 2, 4)
	fmt.Printf("sli2 value is %v\n",sli2)
	fmt.Printf("sli2 len is %v\n", len(sli2))
	fmt.Printf("sli2 cap is %v\n",cap(sli2))
	/*
	sli2 value is [0 0]
	sli2 len is 2
	sli2 cap is 4
	*/

	//声明切片
	sli3 := []int {
    
    1,2,3,4,5,6,7}
	sli3_c1 := sli3[0:2]//如果从0开始切的话,容量将保留原来的大小,即尾部不变,开始可变
	sli3_c2 := sli3[1:2]

	fmt.Printf("sli3 value is %v\n",sli3)
	fmt.Printf("sli3 len is %v\n", len(sli3))
	fmt.Printf("sli3 cap is %v\n",cap(sli3))

	fmt.Printf("sli3_c1 value is %v\n",sli3_c1)
	fmt.Printf("sli3_c1 len is %v\n", len(sli3_c1))
	fmt.Printf("sli3_c1 cap is %v\n",cap(sli3_c1))

	fmt.Printf("sli3_c2 value is %v\n",sli3_c2)
	fmt.Printf("sli3_c2 len is %v\n", len(sli3_c2))
	fmt.Printf("sli3_c2 cap is %v\n",cap(sli3_c2))
	/*
	sli value is [1]
	sli len is 1
	sli cap is 3
	sli2 value is [0 0]
	sli2 len is 2
	sli2 cap is 4
	sli3 value is [1 2 3 4 5 6 7]
	sli3 len is 7
	sli3 cap is 7
	sli3_c1 value is [1 2]
	sli3_c1 len is 2
	sli3_c1 cap is 7
	sli3_c2 value is [2]
	sli3_c2 len is 1
	sli3_c2 cap is 6
	*/

	//切片添加元素
	new_append := append(sli3, 10)
	fmt.Printf("new_append value is %v\n",new_append)
	fmt.Printf("new_append len is %v\n", len(new_append))
	fmt.Printf("new_append cap is %v\n",cap(new_append))
	/*
	new_append value is [1 2 3 4 5 6 7 10]
	new_append len is 8
	new_append cap is 14     #翻倍了
	*/

	//列表
	tmpList := list.New()
	for i:=1; i<10;i++{
    
    
		tmpList.PushBack(i)
	}

	first_List := tmpList.PushFront(0)
	tmpList.Remove(first_List)
	for i:=tmpList.Front(); i != nil;i=i.Next(){
    
    
		fmt.Print(i.Value,"\n")
	}

	//字典
	diction := make(map[int]string)
	diction[0] = "xx"
	diction[1] = "yy"
	diction[3] = "zz"

	diction2 := map[int]string{
    
    
		0:"aa",
		1:"bb",
		2:"cc",
	}

	fmt.Printf("id %v is %v\n",2,diction2[2])

	//遍历
	nums := [...]int{
    
    1,2,3,4,5,6,7,8,9}
	for k,v := range nums{
    
    
		fmt.Println(k,v,"")
	}

	tmpMap := map[int]string{
    
    
		0:"aa",
		1:"bb",
		2:"cc",
	}
	for k,v := range tmpMap{
    
    
		fmt.Println(k,v,"")
	}
}

//命名返回值方法,需要写return,但是不可与非命名返回值混合使用
func div_func_test(dividend ,divisor int)(quotient, remainder int){
    
    
	quotient = dividend / divisor
	remainder = dividend % divisor
	//number := 10
	//number2 := 1
	//return number,number2 	//方式1,写返回变量
	return	//方式2 ,不写返回变量
}

//匿名函数方法,作为入参
func proc(input string, processor func(str string)){
    
    
	processor(input)
}

//闭包发生器,返回一个闭包
func createCounter(initial int) func() int{
    
    
	if initial < 0{
    
    
		initial = 0
	}

	return func()int{
    
    
		initial++
		return initial
	}
}

//接口声明
type Tank interface {
    
    
	Walk()
	Fire()
}

type Plane interface {
    
    
	Fly()
}

//接口和嵌套
type PlaneTank interface {
    
    
	Tank
	Plane
}

type Cat interface {
    
    
	CatchMouse()
}

type Dog interface {
    
    
	Bark()
}

type CatDog struct {
    
    
	Name string
}

func (catDog *CatDog)CatchMouse(){
    
    
	fmt.Printf("I am %v CatchMouse\n",catDog.Name)
}

func (catDog *CatDog)Bark(){
    
    
	fmt.Printf("I am %v Bark\n",catDog.Name)
}

type Printer interface {
    
    
	Print(interface{
    
    })
}

//定义类型
type FuncCaller func(p interface{
    
    })
func (FuncCaller FuncCaller) Print(p interface{
    
    }){
    
    
	FuncCaller(p)
}

type Person struct {
    
    
	Name string
	Brith string
	ID int64
}

func struct_test(){
    
    
	//声明实例化
	var xm Person
	xm.Name = "xiaoming"
	xm.Brith = "1995-08-09"
	xm.ID = 4402200000000

	//new函数实例化
	zw := new(Person)
	zw.Name = "zhangwei"
	zw.Brith = ""

	//取址实例化
	ww := &Person{
    
    }
	ww.Name = "wanwu"

	//初始化1
	sb := Person{
    
    
		Name: "",
		Brith: "",
		ID: 777,
	}
	sb.ID += 1

	sd := &Person{
    
    
		"沙雕",
		"1995-4-3",
		666,
	}

	fmt.Print(sd)
	sd.changeName("新的名字")
	sd.printMesg("打印")

	var myDog CatDog
	myDog.Name = "xiaogou"
	myDog.CatchMouse()
	myDog.Bark()
}

//指针接收器,操作的是原来的内存
func (person *Person)changeName(name string){
    
    
	person.Name = name
	fmt.Printf("My name is %v\n",person.Name)
}

//非指针接收器,操作的是拷贝的内存
func(person Person)printMesg(name string){
    
    
	person.Name = name
	fmt.Printf("My name is %v\n",person.Name)
}

//内嵌和组合
type Wheel struct {
    
    
	shape string
}

type Car struct {
    
    
	Wheel 	//直接定义类型,不定义对象
	Name string
}

func main() {
    
    
	fmt.Printf("Main running...\n")
	//pack_flag_test()
	//pack_point_operate()
	//pack_alias_test()
	//if_switch_test("DD",22)
	//container_test()
	/*
	ret1,ret2 := div_func_test(20,3)
	fmt.Printf("ret1:%v, ret2:%v\n",ret1,ret2)
	 */

	//匿名函数方法1
	func (name string){
    
    
		fmt.Printf("My name is %s\n",name)
	}("zz")

	//匿名函数方法2
	currentTime := func(){
    
    
		fmt.Println(time.Now())
	}
	currentTime()

	//匿名函数方法3,函数参数
	proc("皮皮",func (str string){
    
    
		for _,v := range str{
    
    
			fmt.Printf("%c\n",v)
		}
	})

	c1 := createCounter(1)
	fmt.Println(c1())
	fmt.Println(c1())

	c2 := createCounter(10)
	fmt.Println(c2())
	fmt.Println(c1())

	//复杂的亚比
	var printer Printer
	//将匿名函数强转为 FuncCaller赋值给printer
	printer = FuncCaller(func(p interface{
    
    }){
    
    
		fmt.Println(p)
	})
	printer.Print("Golang is good!")

	struct_test()
	car := &Car{
    
    
		Wheel{
    
    
			"圆形的",
		},
		"福特",
		}
	fmt.Println(car.Name,car.shape,"")
}

猜你喜欢

转载自blog.csdn.net/qq_40904479/article/details/106459801