当前位置: 首页 > news >正文

Go基础学习笔记-知识点

学习笔记记录了我在学习官方文档过程中记的要点,可以参考学习。

go build *.go 文件 编译
go run *.go 执行
go mod init 生成依赖管理文件
gofmt -w *.go 格式换
  • 名称的大小写用来控制方法的可见域
  • 主方法及包命名规范
package main //注意package的命名,作为主包
import "fmt"
func main() {fmt.Println("hello word")
}
  • 初始化mod文件
go mod init example/hello
  • 运行
go run .
  • 查看环境变量
     
go env
  • 添加别的moudle,如果使用了go.work指明了工作空间,则不需要再执行命令添加本地moudle
go mod edit -replace learn/greetings=../greetings
go mod tidy
  • go.work文件用于标明工作空间
go 1.22.0use(./basic./greetings
)
  • you initialize a map with the following syntax: make(map[key-type]value-type)

  • 单元测试

    Test function names have the form TestName, 
  • 查看已经安装的包

D:\1workspace_go\greetings>go list
learn/greetingsD:\1workspace_go\greetings>go list all
  •  下载依赖
go get .
或
go get example.com/theirmodulego get example.com/theirmodule@v1.3.4
go get example.com/theirmodule@latestTo get the module at a specific commit, append the form @commithash:
$ go get example.com/theirmodule@4cf76c2To get the module at a specific branch, append the form @branchname:
$ go get example.com/theirmodule@bugfixes
  • 修改依赖下载源

GOPROXY="https://proxy.golang.org,direct"
不使用proxy下载
The GOPRIVATE or GONOPROXY environment variables may be set to 
lists of glob patterns matching module prefixes 
that are private and should not be requested from any proxy. 
For example:
GOPRIVATE=*.corp.example.com,*.research.example.com
  • 查看依赖版本更新
go list -m -u all
go list -m -u example.com/theirmodule
  •  语法学习
https://golang.google.cn/tour
  •  结构体是便于不同类型数据封装的结构,它也是一种值类型。区别与pointer
  • 数组

func main() {var a [2]stringa[0] = "Hello"a[1] = "World"fmt.Println(a[0], a[1])fmt.Println(a)primes := [6]int{2, 3, 5, 7, 11, 13}fmt.Println(primes)
}
  •  遍历数组
package mainimport ("math/rand""golang.org/x/tour/pic"
)func Pic(dx, dy int) [][]uint8 {pic := make([][]uint8, dx)for x := range pic {pic[x] = make([]uint8, dy)for y := range pic[x] {pic[x][y] = uint8(rand.Intn(255))}}return pic
}func main() {pic.Show(Pic)
}
  •  遍历map
 for key, value := range myMap {fmt.Println("Key:", key, "Value:", value)}
  •  闭包典例
package mainimport "fmt"// fibonacci is a function that returns
// a function that returns an int.
func fibonacci() func() int {le:=-1ri:=1return func()int{fib:=le+rile=riri=fibreturn fib}
}func main() {f := fibonacci()for i := 0; i < 10; i++ {fmt.Println(f())}
}

http://www.lryc.cn/news/296196.html

相关文章:

  • jvm几个常见面试题整理
  • ReentrantLock 和 公平锁
  • 使用Postman做API自动化测试
  • 入门指南|Chat GPT 的兴起:它如何改变数字营销格局?
  • 【C#】.net core 6.0 创建默认Web应用,以及默认结构讲解,适合初学者
  • Linux中的numactl命令指南
  • AD域国产替代方案,助力某金融企业麒麟信创电脑实现“真替真用”
  • 抽象springBoot报错
  • Linux的打包压缩与解压缩---tar、xz、zip、unzip
  • 在angular12中proxy.conf.json中配置详解
  • PyTorch 中音频信号处理库torchaudio的详细介绍
  • OpenAI研究揭示:ChatGPT对生物武器制造影响有限
  • IntelliJ IDEA 2023.3发布,AI 助手出世,新特性杀麻了!!
  • async 与 await(JavaScript)
  • GPT-1, GPT-2, GPT-3, GPT-3.5, GPT-4论文内容解读
  • 第62讲商品搜索动态实现以及性能优化
  • 我的PyTorch模型比内存还大,怎么训练呀?
  • HTTP协议笔记
  • 零基础学Python之网络编程
  • 09 AB 10串口通信发送原理
  • [145] 二叉树的后序遍历 js
  • 开源模型应用落地-业务优化篇(四)
  • MySQL的MVCC机制
  • stable-diffusion | v1-5-pruned.ckpt和v1-5-pruned-emaonly.ckpt的区别
  • 基于Springboot的足球社区管理系统(有报告)。Javaee项目,springboot项目。
  • 8.0 Zookeeper 四字命令教程详解
  • 【MySQL】学习和总结DCL的权限控制
  • React+Antd实现表格自动向上滚动
  • 网络安全产品之认识准入控制系统
  • Text2SQL研究-Chat2DB体验与剖析