云起工作室 15711107967
基础笔记
2025-02-06 10:37:49
linux下安装go
先安装epel:yum install epel-release
然后再安装golang:yum install golang

或者
rpm --import https://mirror.go-repo.io/centos/RPM-GPG-KEY-GO-REPO
curl -s https://mirror.go-repo.io/centos/go-repo.repo | tee /etc/yum.repos.d/go-repo.repo
yum install golang

后台启动项目
go build main.go 生成  main 文件
nohup ./main  &   后台启动命令
ps aux |grep main  找到进程
kill -9  进程id


一、array  数组

	slice 是个概念,不是函数

二、map	对象

	2.1、初始化时需用make

		arr:=make(map[int]string)

	2.2、批量赋值时用不用make 

		arr:=map[string]string{"name":"jim","age":"15"}

	2.3、复合map

		arr:make(map[string]map[int]string)

		arr['name'] = make(map[int]string)

		arr['name'][1] = "hello" 




三、function  关键字func

	不支持嵌套、重载和默认参数

四、struct

	type person struct{

		name string

		age int

	}

	func main(){

		a:=person{name:"jim",age:18}

	} 




五、method

	任何类型数据都可以绑定方法

	例1:

	type person struct{

		name stype

	}

	func (man person) act{

		fmt.Println("hello")

	}

	func main(){

		man:=person{}

		main.act()  // 输出  hello

	}

	例2:

	type num int

	func (n *num) act{

		fmt.Println("hello")

	}

	func main(){

		var n num

		n.act() //输出hello 

	}

	

六、接口interface

	import  "reflect"

	type User struct{

		Id int

		Name string

		Age int

	}




	func (u User) Hello(){

		fmt.Println("hello")

	}

	//不能引用传参

	func Info(o interface{}){

		t:=reflect.TypeOf(o)

		

		if k:=t.Kind();k!=reflect.Struct {

			return

		}




		fmt.Println("Type:",t.Name())

		fmt.Println(t.NumField())

		v:=reflect.ValueOf(o)

		// 获取成员信息

		for i:=0;i<t.NumField();i++{

			f:=t.Field(i)

			val:=v.Field(i).Interface()

			fmt.Printf("%6s: %v = %v\n",f.Name,f.Type,val)

		}

		// 获取方法信息

		for i:=0;i<t.NumMethod(); i++{

			m:=t.Method(i)

			fmt.Printf("%6s: %v\n",m.Name,m.Type)

		}

	} 

	func main(){

		u:=User{1,"jim",12}

		Info(u)

	}




七、反射reflection




八、并发concurrency

	

	func main(){

		c:=make(chan bool)  //无缓存是阻塞的,只定义或有缓存不阻塞  <-c  使其无缓存,进行阻塞

		go func(){

			fmt.Println("go go go!!!")

			c <- true

		}

		<- c

	}




8.1、例1

	import (

		"fmt"

		"runtime"

	)

	func main(){

		runtime.GOMAXPROCS(runtime.NumCPU())  //分配最大核数cpu

		c :=make(chan bool,10)

		for i:=0;i<10;i++{

			go Go(c)

		}

		for i:=0;i<10;i++{

			<-c

		}

	}

	func Go(c chan bool){

		fmt.Println("go go go!!!")

		c<- true

	}

8.2、例2

	import (

		"fmt"

		"runtime"

		"sync"

	)

	func main(){

		runtime.GOMAXPROCS(runtime.NumCPU())  //分配最大核数cpu

		wg:=sync.WaitGroup{}

		wg.Add(10)

		for i:=0;i<10;i++{

			go Go(&wg)

		}

		wg.Wait()

	}

	func Go(wg *sync.WaitGroup){

		fmt.Println("go go go!!!")

		wg.Done()

	} 




随机产生数字













































code := fmt.Sprintf( "%06v", rand.New(rand.NewSource( time.Now().UnixNano() )).Int3ln(1000000) )