Golang基础1

Golang 教程1

注意,该文档只适合有编程基础的同学,这里的go教程只给出有区别的知识点

[go教程]('Go基础 · Go语言中文文档 (topgoer.com)')

一、基础

tip: 下划线_ 可以起到忽略的作用

运行以下命令来初始化一个新的 Go 模块:

go 复制代码
// 如你的项目在 gocode/testproject01/unit2/demo01 中 (从src后开始, 用'/'隔开)。
go mod init gocode/testproject01/unit2/demo01 
//1、go mod init:这是 Go 的模块初始化命令,用于创建一个新的 go.mod 文件,它是 Go 模块的元数据文件,用于记录模块的基本信息和依赖关系。
//2、gocode/testproject01/unit2/demo01:这是你指定的模块名称。这个名称将成为你的模块在 Go 生态系统中的唯一标识符。


//输入操作
_, err := fmt.Scanf("%v", &name) //录入数据的时候,类型一定要匹配。

// _:这是一个空白标识符,在 Go 语言中用来忽略赋给它的值。 
//1、 fmt.Scanln 函数返回两个值:第一个是扫描的元素数量,第二个是错误(如果有)。因为我们只关心错误(第二个返回值),所以我们使用 _ 来忽略第一个返回值。
//2、 err:这是用来存储 fmt.Scanln 函数返回的错误值。如果函数执行成功,则 err 将为 nil;如果发生错误,则 err 将包含具体的错误信息。
//3、 :=:这是一个短声明语法,用于声明并初始化一个变量。在这里,我们声明了一个名为 err 的变量,并将其初始化为 fmt.Scanln(&a) 函数的第二个返回值。
//4、 fmt.Scanln(&a):这是 fmt.Scanln 函数调用,它从标准输入读取数据,并尝试将读取的数据转换成变量 a 的类型(这里是整型 int)。它需要一个变量的引用作为参数,这里使用了 &a。

1、 自动缩进

go 复制代码
gofmt -w xxx.go 
2、 关于赋值, 支持这种声明
go 复制代码
sex := "man"
var n4, name, n5 = 10, "jack", 7.8
n6, height := 6.9, 100.6
3、全局赋值, 省时省力
go 复制代码
var (
	n9 = 100
	n10 = "dasda"
)

//导入包也可以如此操作, 如
import (
    "fmt"
    "strconv"
)
4、字符串一旦定义好,值不可变
go 复制代码
name = "黎明"
name[0] = "牛" //会报错
5、 如果字符串需要赋值特殊的字符(关键字、<、= ...),用反单引号包裹
go 复制代码
str := `
	var a string = 1
	string a < 1231
`
6、类型转换 T() 显示转化, T为你想要转换的类型
go 复制代码
a := 100
b := float32(a)
fmt.Println(a, b)
//注意,类型大的转化至类型小的变量,高位部分会被截断

var n1 int64 =  888888888
var n2 = int8(n1)
fmt.Println(n2)
fmt.Printf("%T", n2c)

``````````````````````````````````````````````````````````````````````````````````````````````````
(base) PS E:\Goproject\src\gocode\testproject01\unit2\demo01> go run .\main.go
56
int8
``````````````````````````````````````````````````````````````````````````````````````````````````

var n3 int32 = 12 
//会报错, var n4 int64 = int64(n3) + 30
var n4 int8 = n3 + 30 
fmt.Println(n3)
fmt.Println(n4)

``````````````````````````````````````````````````````````````````````````````````````````````````
(base) PS E:\Goproject\src\gocode\testproject01\unit2\demo01> go run .\main.go
# command-line-arguments
.\main.go:5:16: cannot use n3 + 30 (value of type int32) as int8 value in variable declaration
``````````````````````````````````````````````````````````````````````````````````````````````````
7、 string类型转化,strconv.parseInt等函数返回的是2个值(value, err)所以应有2个变量承接
go 复制代码
func main() {
	str := "123"
	i, err := strconv.Atoi(str)
	if err != nil {
		fmt.Println(err)
	} else {
		fmt.Println(i)
	} 
}

``````````````````````````````````````````````````````````````````````````````````````````````````
(base) PS E:\Goproject\src\gocode\testproject01\unit2\demo01>  go run .\main.go
123
``````````````````````````````````````````````````````````````````````````````````````````````````
8、 指针
go 复制代码
func main() {
	var a int = 10
	var ptr *int = &a
	fmt.Println("Value of a is ", a)
	fmt.Println("Value of a is ", *ptr)
	fmt.Println("Address of a is ", ptr)
	fmt.Println("Address of a is ", &a)
	fmt.Println("Address of a is ", &ptr)
	*ptr = 20
	fmt.Println("Value of a is ", a)
} 

``````````````````````````````````````````````````````````````````````````````````````````````````
(base) PS E:\Goproject\src\gocode\testproject01\unit2\demo01>  go run .\main.go  
Value of a is  10
Value of a is  10
Address of a is  0xc00000a0f8
Address of a is  0xc00000a0f8
Address of a is  0xc000078050
Value of a is  20
`````````````````````````````````````````````````````````````````````````````````````````````````
9、分支控制
go 复制代码
//单、多分支, 注意:条件表达式可以不写,也建议不写
if 条件表达式 {
   逻辑代码
} else if 条件表达式 {
   逻辑代码
} else {
   逻辑代码 
}

// switch 不用额外加break 防止代码穿透
switch 条件表达式 {
    case 条件满足1:
    	xxx
    case 条件满足2:
    	xxx
    。。。
    default:
    	XXX
}

// 如:
func main() {
	var a int 
	fmt.Println("请输入数字:")
	_, err := fmt.Scanln(&a)
	if err != nil {
		fmt.Println("输入错误:", err)
		return
	}
	if a == 1 {
		fmt.Println("a = 1")
	} else if a == 2 {
		fmt.Println("a = 2")
	} else {
		fmt.Println("a = 3")
	}

} 

//可以使用 fallthrough 穿透,但只能穿透一层
10、 循环 (go里面没有while循环)
go 复制代码
//跟c语言一样,条件表达式不用()包裹
func main() {
	i, sum := 0, 0 
	for i = 0; i < 10; i++ {
		sum += i
	}
	fmt.Println(sum)
} 
``````````````````````````````````````````````````````````````````````````````````````````````````
(base) PS E:\Goproject\src\gocode\testproject01\unit2\demo01> go  run .\main.go
45
``````````````````````````````````````````````````````````````````````````````````````````````````
//可以跟while一样, 格式灵活
func main() {
	for {
		fmt.Println("hello Golang")
	}
} 
``````````````````````````````````````````````````````````````````````````````````````````````````
(base) PS E:\Goproject\src\gocode\testproject01\unit2\demo01> go  run .\main.go
hello Golang
hello Golang
hello Golang
hello Golang
hello Golang
。。。
``````````````````````````````````````````````````````````````````````````````````````````````````
10.1 for range (Go 特有,可以遍历数组、切片、字符串、map以及通道,形式上有点类似于foreach)
go 复制代码
func main() {
	str := "hello world"
	for i := 0; i < len(str); i++ {
		fmt.Printf("%c", str[i])
	}
} 

//等效于
// 这里的 fmt.Printf("索引为 %d, 对应的值为 %c \n", i, val) 是带格式的输出, 可以类比 fmt.Println()
func main() {
	str := "hello world"
	for i, val  := range str {
		fmt.Printf("索引为 %d, 对应的值为 %c \n", i, val)
	}
} 
相关推荐
陌小呆^O^10 分钟前
Cmakelist.txt之win-c-udp-client
c语言·开发语言·udp
ifanatic13 分钟前
[面试]-golang基础面试题总结
面试·职场和发展·golang
懒是一种态度20 分钟前
Golang 调用 mongodb 的函数
数据库·mongodb·golang
I_Am_Me_26 分钟前
【JavaEE进阶】 JavaScript
开发语言·javascript·ecmascript
重生之我是数学王子36 分钟前
QT基础 编码问题 定时器 事件 绘图事件 keyPressEvent QT5.12.3环境 C++实现
开发语言·c++·qt
Ai 编码助手38 分钟前
使用php和Xunsearch提升音乐网站的歌曲搜索效果
开发语言·php
学习前端的小z42 分钟前
【前端】深入理解 JavaScript 逻辑运算符的优先级与短路求值机制
开发语言·前端·javascript
神仙别闹1 小时前
基于C#和Sql Server 2008实现的(WinForm)订单生成系统
开发语言·c#
XINGTECODE1 小时前
海盗王集成网关和商城服务端功能golang版
开发语言·后端·golang
入 梦皆星河1 小时前
在 Ubuntu/Debian 上安装 Go
ubuntu·golang·debian