Go语言基础工程目录如下(采用LiteIDE):
备注:需要提前设置GOPATH,即工作目录,bin、pkg、src为默认的GO工程目录结构。GOPATH可以支持多个,在windows下配置在环境变量里,以;分割。
go install com.fredric\lib 命令编译库
go install com.fredric\main 命令编译出执行文件main.exe
实际项目中运用了GO语言,抽时间夯实基础,在lib中实现了一些测试函数,如下:
1、集合相关
package lib import ( "fmt" ) //测试数组 func ArrayTest() { var array = []uint8{1, 2, 3} var i int = 0 for i = 0; i < len(array); i++ { fmt.Printf("array %d Element:%d\n", i, array[i]) } } //测试结构体 type User struct { UserName string PassWord string } func StructTest() { var user1 User user1.UserName = "Fredric" user1.PassWord = "Sinny" fmt.Printf("UserName: %s And Password %s\n", user1.UserName, user1.PassWord) //结构体指针 var pUser1 *User pUser1 = &user1 fmt.Printf("UserName: %s And Password %s\n", pUser1.UserName, pUser1.PassWord) } //测试切片 func SliceTest() { //切片可以理解为可变数组 var slice1 []uint8 = make([]uint8, 5, 10) //5为初始长度、10为容量,容量是可选参数,初始值为0 fmt.Printf("slice1:%v Len:%d Cap:%d\n", slice1, len(slice1), cap(slice1)) //此时LEN和CAP都为6 slice1 = []uint8{1, 2, 3, 4, 5, 6} fmt.Printf("slice1:%v Len:%d Cap:%d\n", slice1, len(slice1), cap(slice1)) //切片可以超出容量 var array = []uint8{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11} slice1 = array fmt.Printf("slice1:%v\n", slice1) //打印结果为3,4,5 fmt.Printf("slice1 2:5 %v", slice1[2:5]) array[0] = 11 //数组的值修改后,切片的值也对应修改,此时slice1[0] = 11 //因此slice是一个指针,指向那段数据内存 fmt.Printf("slice1:%v [0]:%d\n", slice1, slice1[0]) //此时slice1尾部增加0,0两个元素,len = 13 CAP = 32 //注意:为什么此处CAP比len大那么多,因为在Slice上追加元素的时候,首先会放到这块空闲的内存中, //如果添加的参数个数超过了容量值,内存管理器会重新划分一块容量值为原容量值*2大小的内存空间 slice1 = append(slice1, 0, 0) fmt.Printf("slice1:%v Len:%d Cap:%d\n", slice1, len(slice1), cap(slice1)) newSlice := make([]uint8, len(slice1), cap(slice1)) copy(newSlice, slice1) fmt.Printf("newSlice:%v Len:%d Cap:%d\n", newSlice, len(newSlice), cap(newSlice)) } //测试MAp func MapTest() { var myMap map[uint8]string myMap = make(map[uint8]string) myMap[0] = "fredric" myMap[1] = "sinny" for item := range myMap { fmt.Printf("No[%d] Element Is %s\n", item, myMap[item]) } item, res := myMap[1] fmt.Printf("%s %t\n", item, res) delete(myMap, 1) item, res = myMap[1] fmt.Printf("%s %t\n", item, res) }
2、函数相关
package lib import ( "fmt" ) //多个参数多个返回值 func test01(param1 string, param2 uint8) (string, bool) { fmt.Printf("Param1 = %s, Param2 = %d\n", param1, param2) return "result", true } //测试函数特性 func FuncTest() { //res1, res2 := test01("hello,world", 6) //fmt.Println(res1, res2) //打印输出24 fmt.Println(Factorial(4)) } //菲波那切数列的递归函数 func Factorial(x int) (result int) { if x == 0 { result = 1 } else { result = x * Factorial(x-1) } return }
3、面向对象相关
package lib import ( "fmt" ) //定义一个接口 type Service interface { doService() getParam() string setParam(input string) } type ServiceImpl1 struct { param string } func (impl1 *ServiceImpl1) doService() { fmt.Println("ServiceImpl1", impl1.param) } func (impl1 *ServiceImpl1) getParam() string { return impl1.param } func (impl1 *ServiceImpl1) setParam(input string) { impl1.param = input } type ServiceImpl2 struct { param string } func (impl2 *ServiceImpl2) doService() { fmt.Println("ServiceImpl2", impl2.param) } func (impl2 *ServiceImpl2) getParam() string { return impl2.param } func (impl2 *ServiceImpl2) setParam(input string) { impl2.param = input } //测试利用接口的面向对象 func OOTest() { var service Service service = new(ServiceImpl1) service.setParam("Fredric") //打印ServiceImpl1 Fredric service.doService() service = new(ServiceImpl2) service.setParam("Sinny") //打印ServiceImpl2 Sinny service.doService() }