当前位置:网站首页>On March 15, the official version of go 1.18 was released to learn about the latest features and usage
On March 15, the official version of go 1.18 was released to learn about the latest features and usage
2022-07-06 12:50:00 【Deng Jiawen jarvan】
3 month 15 Number Go 1.18 Official release Learn about the latest features and usage
linux Download and install go1.18
(1) download
curl -o go1.18.linux-amd64.tar.gz https://dl.google.com/go/go1.18.linux-amd64.tar.gz
(2) new go Version folder
( Here you can replace it with the directory you want )
mkdir ~/go1.18
(3) Unzip the file to the new version folder
( Here you can replace it with the directory you want )
tar zxvf go1.18.linux-amd64.tar.gz -C ~/go1.18
(4) Modify the environment variable to the new version
vim /etc/profile
# root directory ( Here you can replace it with the directory you want )
export GOROOT=$HOME/go1.18
#bin Catalog
export GOBIN=$GOROOT/bin
# working directory
export GOPATH=/root/src.go
export PATH=$PATH:$GOPATH:$GOBIN:$GOROOT
(5) Refresh the environment variable configuration
source /etc/profile
(6) see go edition
$ go version
go version go1.18 linux/amd64
The update is successful
Possible problems 1: Environment variables are reset after reconnection
bash Configure the environment variable to ~/.profile perhaps ~/.bash_profile
zsh Configure the environment variables ~/.zshrc
Because reopening the terminal will automatically execute them
(7) vscode Need to be reinstalled go tools
In general use vscode + ssh Remote development
go After the version is updated, the original go tools It may not be available , We need to re install go tools
Click Settings ->command-pallet…( Command Panel )-> Input go tools -> Choose all -> update


Generic generic
Generic small case : Multiple types of support hashmap
func Test Generic generic(t *testing.T) {
//[string,string] Type of 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] Type of 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
}
Fuzzy testing fuzzing
fuzzing testing The design of fuzzy tests, like generics, has existed for a long time
fuzzing testing Fuzzy testing / Random testing will test the reliability of software randomly or according to the initial data of developers and continuously
go test The current members of the tool chain are : unit testing test、 Performance benchmarking bench, Fuzzy testing fuzzing
Fuzzy test case
package main
import (
"fmt"
"testing"
"unicode/utf8"
"github.com/stretchr/testify/assert"
)
// String inversion function ( To be tested )
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)
}
// General unit testing
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)
}
}
// Fuzzy testing , And unit testing complement each other
// go test -fuzz=Fuzz -run ^FuzzReverse$ -v
// Will generate testdata There are crash test data ,Fial The data of
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)
}
})
}
work area workspace
for instance
I want to update a tool module tools, And see the effect of this module update in the project , The general practice is to modify go.mod file , Use replace github Upper tool Replace the library to its local directory , Then the effect of local modification can be reflected in the project in real time
After the module is modified successfully , If this project does not go.mod If you change it back, you must have failed to compile
Workspace mode workspace Can be in go.mod The parent directory of encapsulates a layer of independent go.work, In this document replace Replace , Do not modify the original project go.mod file
Example workspace
The project directory is as follows
workspace-demo
├── project
│ ├── go.mod // Project modules ,mod Sub module
│ └── main.go
├── go.work // work area ,work Upper module
└── tools
├── fish.go
└── go.mod // Tool module ,mod Sub module
Initialize workspace
# Create a new workspace folder
mkdir workspace-demo
cd workspace-demo
# Cloning project project And developed modules tools, For reference only , Please replace with the appropriate git Warehouse
git clone https://github.com/me/project
git clone https://github.com/me/tools
# Initialize workspace
go work init ./project ./tools
modify go.work also replace long-range tools Module to local
go 1.18
use (
./project
./tools
)
// Modify the remote module to replace it locally
replace github.com/me/tools => ./tools
Performance improvement
Apple M1、ARM64 and PowerPC64 Users will be delighted ! because Go 1.17 The register of ABI Calling convention Extend to these architectures ,Go 1.18 Of CPU Performance improvements of up to 20%. To emphasize the performance improvement of this version , We will 20% Performance improvement of As the fourth most important title
About 1.18 A more detailed description of everything in , Please refer to Go 1.18 Release notes .
reference
Go 1.18 Release Notes - The Go Programming Language (golang.org)
Linux On Golang Version update _ Online ghost blog -CSDN Blog
The official tutorial :Go Introduction to generics - SegmentFault Think no
Go 1.18 Version released | Tony Bai
Go 1.18 New features look forward to : Native support Fuzzing test (qq.com)
Go 1.18 New features look forward to :Go Workspace mode | Tony Bai
Go1.18 New characteristics : many Module Workspace mode -51CTO.COM
边栏推荐
- 2021.11.10汇编考试
- FairyGUI简单背包的制作
- [leetcode622] design circular queue
- [算法] 剑指offer2 golang 面试题3:前n个数字二进制形式中1的个数
- [leetcode622]设计循环队列
- Fairygui joystick
- MySQL performance tuning - dirty page refresh
- (1) Introduction Guide to R language - the first step of data analysis
- 燕山大学校园网自动登录问题解决方案
- Get the position of the nth occurrence of the string
猜你喜欢
随机推荐
第一人称视角的角色移动
Unity3d camera, the keyboard controls the front and rear left and right up and down movement, and the mouse controls the rotation, zoom in and out
MySQL replacement field part content
Special palindromes of daily practice of Blue Bridge Cup
wsl常用命令
2021.11.10 compilation examination
音乐播放(Toggle && PlayerPrefs)
微信小程序开发心得
(课设第一套)1-5 317号子任务 (100 分)(Dijkstra:重边自环)
Esp8266 connect onenet (old mqtt mode)
[算法] 剑指offer2 golang 面试题12:左右两边子数组的和相等
idea问题记录
NRF24L01 troubleshooting
FairyGUI循環列錶
Unity场景跳转及退出
rtklib单点定位spp使用抗差估计遇到的问题及解决
Unity3D制作注册登录界面,并实现场景跳转
Unity3D,阿里云服务器,平台配置
The master of double non planning left the real estate company and became a programmer with an annual salary of 25W. There are too many life choices at the age of 25
Expected value (EV)





![[算法] 剑指offer2 golang 面试题10:和为k的子数组](/img/63/7422489d09a64ec9f0e79378761bf1.png)
![[算法] 剑指offer2 golang 面试题2:二进制加法](/img/c2/6f6c3bd4d70252ba73addad6a3a9c1.png)
![[算法] 剑指offer2 golang 面试题13:二维子矩阵的数字之和](/img/17/e7c9bfa867030af97eb66a7932c7e3.png)
