当前位置:网站首页>3月15号 Go 1.18 正式版发布 了解最新特色以及使用方法
3月15号 Go 1.18 正式版发布 了解最新特色以及使用方法
2022-07-06 09:18:00 【邓嘉文Jarvan】
3月15号 Go 1.18 正式版发布 了解最新特色以及使用方法
linux 下载安装 go1.18
(1)下载
curl -o go1.18.linux-amd64.tar.gz https://dl.google.com/go/go1.18.linux-amd64.tar.gz
(2)新的go版本的文件夹
(这里可以替换为你想要的目录)
mkdir ~/go1.18
(3)解压文件到新版本文件夹
(这里可以替换为你想要的目录)
tar zxvf go1.18.linux-amd64.tar.gz -C ~/go1.18
(4)修改环境变量为新版本
vim /etc/profile
#根目录(这里可以替换为你想要的目录)
export GOROOT=$HOME/go1.18
#bin目录
export GOBIN=$GOROOT/bin
#工作目录
export GOPATH=/root/src.go
export PATH=$PATH:$GOPATH:$GOBIN:$GOROOT
(5)刷新环境变量配置
source /etc/profile
(6) 查看go版本
$ go version
go version go1.18 linux/amd64
更新成功
可能出现的问题1: 环境变量重新连接后被重置
bash的话将环境变量配置到 ~/.profile
或者~/.bash_profile
zsh的话将环境变量配置 ~/.zshrc
因为重开终端会自动执行他们
(7) vscode 需要重新安装 go tools
一般使用 vscode + ssh 远程开发
go版本更新后原来的 go tools 可能不可用, 我们需要重新安装 go tools
点击设置->command-pallet…(命令面板)->输入 go tools -> 全部选择 -> update
泛型 generic
泛型小案例: 多类型支持的hashmap
func Test泛型generic(t *testing.T) {
//[string,string] 类型的 hashmap
hashmap1 := &HashMap[string, string]{
hashmap: make(map[string]string)}
hashmap1.Set("k1", "v1")
value, _ := hashmap1.Get("k1")
fmt.Printf("value2: %v,type=%T\n", value, value)
//[string,int] 类型的 hashmap
hashmap2 := &HashMap[string, int]{
hashmap: make(map[string]int)}
hashmap2.Set("k1", 1)
value2, _ := hashmap2.Get("k1")
fmt.Printf("value2: %v,type=%T\n", value2, value2)
// value2: v1,type=string
// value2: 1,type=int
}
type HashMap[K comparable, V any] struct {
hashmap map[K]V
}
func (h *HashMap[K, V]) Set(key K, value V) {
h.hashmap[key] = value
}
func (h *HashMap[K, V]) Get(key K) (value V, ok bool) {
value, ok = h.hashmap[key]
return value, ok
}
模糊测试 fuzzing
fuzzing testing 模糊测试的设计和泛型一样存在了很长时间
fuzzing testing 模糊测试/随机测试会随机或者根据开发人员的初始数据随机数据并持续性的测试软件的可靠性
go test工具链目前的成员有: 单元测试test、性能基准测试bench, 模糊测试fuzzing
模糊测试小案例
package main
import (
"fmt"
"testing"
"unicode/utf8"
"github.com/stretchr/testify/assert"
)
//字符串反转函数(待测试)
func Reverse(s string) string {
b := []byte(s)
for i, j := 0, len(b)-1; i < len(b)/2; i, j = i+1, j-1 {
b[i], b[j] = b[j], b[i]
}
return string(b)
}
//普通单元测试
func TestReverse(t *testing.T) {
testcases := []struct {
in, expect string
}{
{
"Hello, world", "dlrow ,olleH"},
{
" ", " "},
{
"!12345", "54321!"},
}
for _, tc := range testcases {
actual := Reverse(tc.in)
assert.Equal(t, tc.expect, actual)
}
}
//模糊测试,和单元测试相辅相成
// go test -fuzz=Fuzz -run ^FuzzReverse$ -v
//会生成 testdata 里面有崩溃的测试数据,Fial的数据
func FuzzReverse(f *testing.F) {
testcases := []string{
"Hello, world", " ", "!12345"}
for _, tc := range testcases {
f.Add(tc) // Use f.Add to provide a seed corpus
}
f.Fuzz(func(t *testing.T, orig string) {
fmt.Printf(".")
rev := Reverse(orig)
doubleRev := Reverse(rev)
if orig != doubleRev {
t.Errorf("Before: %q, after: %q", orig, doubleRev)
}
if utf8.ValidString(orig) && !utf8.ValidString(rev) {
t.Errorf("Reverse produced invalid UTF-8 string %q", rev)
}
})
}
工作区 workspace
举个例子
我要更新一个工具模块 tools, 并在项目中看这个模块更新的效果, 一般的做法是修改 go.mod 文件, 使用 replace github上的tool库替换到自己的本地目录, 然后本地修改的效果就能实时在项目中体现了
模块修改成功后, 如果这个项目不将 go.mod 改回来的话推上去的一定是编译失败的
工作区模式 workspace 可以在在 go.mod 的上级目录封装一层独立的 go.work, 在这个文件里面 replace 替换,不要修改原有项目的 go.mod 文件
工作区示例
项目目录如下
workspace-demo
├── project
│ ├── go.mod // 项目模块,mod子模块
│ └── main.go
├── go.work // 工作区,work上层模块
└── tools
├── fish.go
└── go.mod // 工具模块,mod子模块
初始化工作区
# 新建工作区文件夹
mkdir workspace-demo
cd workspace-demo
# 克隆项目 project 和开发的模块 tools, 这里仅作参考,请替换为合适的 git 仓库
git clone https://github.com/me/project
git clone https://github.com/me/tools
# 初始化工作区
go work init ./project ./tools
修改 go.work 并且 replace 远程 tools 模块到本地
go 1.18
use (
./project
./tools
)
//修改远程模块为本地替换
replace github.com/me/tools => ./tools
性能提升
苹果M1、ARM64和PowerPC64用户肯定会欢欣鼓舞! 由于Go 1.17的寄存器ABI调用约定扩展到这些架构,Go 1.18的CPU性能提升幅度高达20%。为了强调这个版本的性能提升幅度,我们将20%的性能改进作为了第四个最重要的标题
关于1.18中的所有内容的更详细描述,请查阅Go 1.18发布说明。
reference
Go 1.18 Release Notes - The Go Programming Language (golang.org)
Linux上Golang 版本升级_线上幽灵的博客-CSDN博客
Go 1.18新特性前瞻:原生支持Fuzzing测试 (qq.com)
边栏推荐
- Unity scene jump and exit
- idea问题记录
- What are the functions and features of helm or terrain
- MySQL replacement field part content
- Get the position of the nth occurrence of the string
- [offer29] sorted circular linked list
- [leetcode15] sum of three numbers
- About using @controller in gateway
- 1041 be unique (20 points (s)) (hash: find the first number that occurs once)
- Expected value (EV)
猜你喜欢
(core focus of software engineering review) Chapter V detailed design exercises
程序设计大作业:教务管理系统(C语言)
Fabrication d'un sac à dos simple fairygui
Esp8266 connect onenet (old mqtt mode)
Excel导入,导出功能实现
Unity3D,阿里云服务器,平台配置
MySQL shutdown is slow
Single chip Bluetooth wireless burning
(四)R语言的数据可视化——矩阵图、柱状图、饼图、散点图与线性回归、带状图
(5) Introduction to R language bioinformatics -- ORF and sequence analysis
随机推荐
Derivation of logistic regression theory
(the first set of course design) 1-4 message passing interface (100 points) (simulation: thread)
Gateway fails to route according to the service name, and reports an error service unavailable, status=503
Expected value (EV)
Mysql database reports an error: row size too large (> 8126) Changing some columns to TEXT or BLOB or using ROW_ FORMAT=DY
Combination of fairygui check box and progress bar
KF UD分解之伪代码实现进阶篇【2】
FairyGUI循環列錶
Fairygui joystick
2021.11.10 compilation examination
Special palindromes of daily practice of Blue Bridge Cup
NovAtel 板卡OEM617D配置步骤记录
Unity3D,阿里云服务器,平台配置
Prove the time complexity of heap sorting
[offer9]用两个栈实现队列
FGUI工程打包发布&导入Unity&将UI显示出来的方式
Gravure sans fil Bluetooth sur micro - ordinateur à puce unique
Programming homework: educational administration management system (C language)
[899]有序队列
Theoretical derivation of support vector machine