8小时快速入门,golang安装学习,有语言基础的快来学习
Posted 黄昏单车
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了8小时快速入门,golang安装学习,有语言基础的快来学习相关的知识,希望对你有一定的参考价值。
安装golang(Linux环境)
# 下载linux源码包中文官网地址
https://studygolang.com/dl
# 下载命令直接下载
wget https://studygolang.com/dl/golang/go1.17.2.linux-amd64.tar.gz
# 解压到指定目录
tar -zxf go1.17.2.linux-amd64.tar.gz -C /usr/local
# 配置go的相关路径,在末尾加入以下内容
vim ~/.bashrc
# 设置go语言路径
export GOROOT=/usr/local/go
# 设置工作路径
export GOPATH=$HOME/go
# 设置系统环境变量
export PATH=$PATH:$GOROOT/bin:$GOPATH/bin
# 刷新配置
source ~/.bashrc
# 检查是否安装go成功
go version 或者 go --help
Golang的优势
Golang适合用来做什么(强项)
Go明星作品(成就)
Golang的不足(缺点)
1 - firstGolang
package main // 程序包名
// 导入包
//import "fmt"
import (
"fmt"
"time"
)
func main() // 跟函数名必须同行,否则编译错误
// 结尾 ; 分号可加可不加,建议不加
fmt.Println("hello Go!")
time.Sleep(1 * time.Second)
2 - 声明 var
package main
import "fmt"
/*
四种变量的声明方式
注意:变量声明必须使用,否则错误
*/
// 声明全局变量 方法一,二,三是可以的
var gA int
var gB int = 10
var gC = 100
// 声明全局变量 方法四,错误:syntax error: non-declaration statement outside function body
// 只能用在函数体内
// gD := 1000
func main()
// 方法一:声明一个变量,默认值是0
var a int
fmt.Println("a = ",a)
fmt.Printf("type of a = %T\\n",a)
// 方法二:声明一个变量,初始化一个值
var b int = 10
fmt.Println("b = ",b)
fmt.Printf("type of b = %T\\n",b)
// 方法三:在初始化的时候,可以省去数据类型,通过自动匹配当前的变量的数据类型
var c = 100
fmt.Println("c = ",c)
fmt.Printf("type of c = %T\\n",c)
// 方法四:(常用的方法)省去var关键字,直接自动匹配
d := 1000
fmt.Println("d = ",d)
fmt.Printf("type of d = %T\\n",d)
e := "abcd"
fmt.Println("e = ",e)
fmt.Printf("type of e = %T\\n",e)
// 全局变量
fmt.Println("gA = ",gA)
fmt.Println("gB = ",gB)
fmt.Println("gC = ",gC)
//fmt.Println("gD = ",gD)
// 多个变量声明方式
var aa,bb int = 10,20
fmt.Println("aa = ",aa,",bb =",bb)
var cc,dd = 100,"acds"
fmt.Println("cc = ",cc,",dd = ",dd)
var (
ee int = 100
ff bool = true
)
fmt.Println("ee = ",ee,",ff = ",ff)
3 - 常量const.iota
package main
import "fmt"
// 定义枚举类
const (
// iota 每行会累加1,第一行默认值是0
BEIJING = iota // iota = 0, BEIJINGJ = 0
SHANGHAI // iota = 1, SHANGHAI = 1
SHENZHEN // iota = 2, SHENZHEN = 2
)
const (
a,b = iota + 1, iota + 2 // iota = 0, a = iota + 1, b = iota + 2, a = 1, b = 2
c,d // iota = 1, c = iota + 1, d = iota + 2, c = 2, d = 3
e,f // iota = 2, e = iota + 1, f = iota + 2, e = 3, f = 4
)
func main()
// 常量(只读属性)
const length = 10
fmt.Println("length = ",length)
// length = 100 // 常量是不允许修改的 报错:cannot assign to length
fmt.Println("BEIJING = ",BEIJING)
fmt.Println("SHANGHAI = ",SHANGHAI)
fmt.Println("SHENZHEN = ",SHENZHEN)
fmt.Println("a = ",a,",b = ",b)
fmt.Println("c = ",c,",d = ",d)
fmt.Println("e = ",e,",f = ",f)
// iota 只允许const()使用 报错:undefined: iota
// var aa = iota
4 - 函数func
package main
import "fmt"
// 返回单个返回值,匿名
func f1(a int, b int) int
fmt.Println("=====f1=====")
fmt.Println("a = ",a,", b = ",b)
//c := 10
return 10
// 返回多个返回值,无形参名称
func f2(a int,b string) (int, string)
fmt.Println("====f2====")
fmt.Println("a = ",a,",b = ",b)
return 100, "abc"
// 返回多个返回值,有形参名称
func f3(a int,b bool) (r1 int, r2 bool)
fmt.Println("====f3====")
fmt.Println("a = ",a,",b = ",b)
// 给有名称的返回值赋值 不赋值默认0 false
r1 = 100
r2 = true
return
// 返回多个返回值,类型相同
func f4(a int, b string) (r1,r2 int)
fmt.Println("====f4====")
fmt.Println("a = ",a,",b = ",b)
return r1,r2
func main()
f1 := f1(1,2)
fmt.Println("f1 = ",f1)
ret1, ret2 := f2(3,"abc")
fmt.Println("ret1 = ",ret1,",ret2 = ",ret2);
ret3, ret4 := f3(100,false)
fmt.Println("ret3 = ",ret3,",ret4 = ",ret4)
//前面已定义的变量,重新赋值,需要类型一致
// 报错:cannot assign int to ret4 (type bool) in multiple assignment
//ret3,ret4 = f4(101,"aaa")
ret5,ret6 := f4(101,"bbb")
fmt.Println("ret5 = ",ret5,",ret6 = ",ret6)
5 - init初始化函数和导包方式
# lib1/lib1.go
package lib1 // 包名和文件夹名称一致
import "fmt"
// 首字母大写代表对外开放api 函数
func Lib1test()
fmt.Println("lib1==Lib1test==")
func init()
fmt.Println("lib1====init===")
# lib2/lib2.go
package lib2
import "fmt"
func Lib2test()
fmt.Println("lib2====Lib2test====")
func init()
fmt.Println("lib2====init===")
# main.go文件
package main
// 注意导包路径是根据工作路径决定,也就是一开始配的 $GOPATH/src/GolangStudy/5-init/lib1
import (
"GolangStudy/5-init/lib1"
"GolangStudy/5-init/lib2"
)
func main()
lib1.Lib1test()
lib2.Lib2test()
package main
import (
// 匿名导入
_ "GolangStudy/5-init/lib1"
// 别名导入
//mylib2 "GolangStudy/5-init/lib2"
// 导入到当前包,慎用,可能会导致方法名冲突
. "GolangStudy/5-init/lib2"
)
func main()
// 当导入包只使用init时,可以采用匿名导入
// lib1.Lib1test()
//lib2.Lib2test()
// 可以使用别名调用
//mylib2.Lib2test()
// 导入当前包可直接使用
Lib2test()
6 - 指针pointer
package main
import "fmt"
/*func swap(a int, b int)
var c int
c = a
a = b
b = c
*/
func swap(a *int, b *int)
var c int
c = *a
*a = *b
*b = c
func main()
a := 10
b := 20
//swap(a, b)
swap(&a, &b)
fmt.Println("a = ",a,",b = ",b);
// 一级指针
var c *int
c = &a
fmt.Println(&a);
fmt.Println(c);
// 二级指针
var d **int
d = &c
fmt.Println(&c);
fmt.Println(d);
7 - defer关键字
package main
import "fmt"
func deferA()
fmt.Println("--A--")
func deferB()
fmt.Println("--B--")
func deferC()
fmt.Println("--C--")
func main()
// 输出: --C-- --B-- --A--
defer deferA()
defer deferB()
defer deferC()
// 输出: --returnFunc-- --deferFunc--
deferAndReturn()
func deferAndReturn() int
defer deferFunc()
return returnFunc()
func deferFunc() int
fmt.Println("--deferFunc--")
return 0
func returnFunc() int
fmt.Println("--returnFunc--")
return 0
8 - 数组array(slice动态数组)
1、定义数组
package main
import "fmt"
func printArr1(arr [4]int)
for index,value := range arr
fmt.Println("index = ",index,",value = ",value)
arr[1] = 100
func printArr2(arr []int)
for index,value := range arr
fmt.Println("index = ",index,",value = ",value)
arr[1] = 100
func main()
// 固定长度数组
var arr1 [10]int
arr2 := [4]int1,2,3,4
//for i := 0; i < 10; i++
for i := 0; i < len(arr1); i++
fmt.Println(arr1[i])
for index, value := range arr2
fmt.Println("index = ",index,",value = ",value)
// 查看数据类型 注意:Printf,不是 Println
fmt.Printf("type of arr1 = %T\\n",arr1)
fmt.Printf("type of arr2 = %T\\n",arr2)
// 不能传长度不一致的数组
// 报错:cannot use arr1 (type [10]int) as type [4]int in argument to printArr
//printArr(arr1)
// 传递的是值
printArr1(arr2)
fmt.Println("arr2[1] = ",arr2[1])
// 不给任何长度,表示动态数组
arr3 := []int11,22,33,44
// 指针传递
printArr2(arr3)
fmt.Println("arr3[1] = ",arr3[1])
2、slice声明的几种方式
package main
import "fmt"
func main()
//声明slice是一个切片,并且初始化,默认值1,2,3,长度len是3
//var slice = []int1,2,3
//slice := []int1,2,3
//声明slice是一个切片,没有分配空间
var slice1 []int
//此时赋值报错
//slice1[0] = 100
//开辟空间再赋值
//slice1 = make([]int,3)
//slice1[0] = 100
//var slice2 []int = make([]int,3)
//var slice2 = make([]int,3)
//slice2 := make([]int,3)
// %v 表示打印详细信息
fmt.Printf("len = %d, slice = %v\\n",len(slice1),slice1)
//判断slice是否为0
if slice1 == nil
fmt.Println("slice1 是一个空切片")
else
fmt.Println("slice1 是有空间的")
3、追加append
package main
import "fmt"
func main()
// 3:长度,5:容量
var number = make([]int,3,5)
fmt.Printf("len = %d, cap = %d, slice = %v\\n",len(number),cap(number),number)
//追加元素1
number = append(number,1)
fmt.Printf("len = %d, cap = %d, slice = %v\\n",len(number),cap(number),number)
//追加元素2,此时容量已满
number = append(number,2)
fmt.Printf("len = %d, cap = %d, slice = %v\\n",len(number),cap(number),number)
//向容量cap已满的slice追加元素3
number = append(number,3)
fmt.Printf("len = %d, cap = %d, slice = %v\\n",len(number),cap(number),number)
fmt.Println("=========")
//切片的扩容机制,append的时候,如果长度增加后超过容量,则将容量增加2倍
var number2 = make([]int,3)
fmt.Printf("len = %d, cap = %d, slice = %v\\n",len(number2),cap(number2),number2)
number2 = append(number2,1)
fmt.Printf("len = %d, cap = %d, slice = %v\\n",len(number2),cap(number2),number2)
8小时快速入门,golang安装学习,有语言基础的快来学习