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.0

use(
    ./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/greetings

D:\1workspace_go\greetings>go list all
  •  下载依赖
go get .
或
go get example.com/theirmodule

go get example.com/theirmodule@v1.3.4
go get example.com/theirmodule@latest

To get the module at a specific commit, append the form @commithash:
$ go get example.com/theirmodule@4cf76c2

To 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]string
	a[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 main

import (
	"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 main

import "fmt"

// fibonacci is a function that returns
// a function that returns an int.
func fibonacci() func() int {
	le:=-1
	ri:=1
	return func()int{
		fib:=le+ri
		le=ri
		ri=fib
		return fib
	}
}

func main() {
	f := fibonacci()
	for i := 0; i < 10; i++ {
		fmt.Println(f())
	}
}

相关推荐

  1. Go基础学习笔记-知识

    2024-02-09 00:10:01       47 阅读
  2. go学习知识

    2024-02-09 00:10:01       40 阅读
  3. TensorRT 自学笔记001 基础知识学习资源

    2024-02-09 00:10:01       66 阅读
  4. Go语言基础知识学习

    2024-02-09 00:10:01       26 阅读
  5. go 基本知识备忘

    2024-02-09 00:10:01       37 阅读
  6. Go基础知识学习-习题题解

    2024-02-09 00:10:01       53 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-02-09 00:10:01       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-02-09 00:10:01       100 阅读
  3. 在Django里面运行非项目文件

    2024-02-09 00:10:01       82 阅读
  4. Python语言-面向对象

    2024-02-09 00:10:01       91 阅读

热门阅读

  1. chrome扩展插件常用文件及作用

    2024-02-09 00:10:01       51 阅读
  2. csapp-chapter3--mov指令

    2024-02-09 00:10:01       53 阅读
  3. 【Nginx介绍和使用——详细讲解】

    2024-02-09 00:10:01       49 阅读
  4. FATFS学习笔记——FATFS写文件的两种方式

    2024-02-09 00:10:01       53 阅读
  5. 记录 | python importlib.import_module()用法

    2024-02-09 00:10:01       47 阅读
  6. 如何清理Docker占用的磁盘空间?

    2024-02-09 00:10:01       44 阅读