当前位置:网站首页>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
边栏推荐
- 抗差估计在rtklib的pntpos函数(标准单点定位spp)中的c代码实现
- Force buckle 1189 Maximum number of "balloons"
- Fabrication d'un sac à dos simple fairygui
- Containers and Devops: container based Devops delivery pipeline
- Fairygui loop list
- [算法] 剑指offer2 golang 面试题5:单词长度的最大乘积
- [Offer29] 排序的循环链表
- 單片機藍牙無線燒錄
- FairyGUI条子家族(滚动条,滑动条,进度条)
- Mysql database reports an error: row size too large (> 8126) Changing some columns to TEXT or BLOB or using ROW_ FORMAT=DY
猜你喜欢

Fairygui loop list

FairyGUI循環列錶

(1) Introduction Guide to R language - the first step of data analysis

Naive Bayesian theory derivation

MySQL shutdown is slow

Programming homework: educational administration management system (C language)

Single chip Bluetooth wireless burning
![[算法] 剑指offer2 golang 面试题13:二维子矩阵的数字之和](/img/17/e7c9bfa867030af97eb66a7932c7e3.png)
[算法] 剑指offer2 golang 面试题13:二维子矩阵的数字之和

dosbox第一次使用
![[Clickhouse kernel principle graphic explanation] about the collaborative work of partitioning, indexing, marking and compressed data](/img/28/221b0a51ef5f2e8ed5aeca2de8f463.jpg)
[Clickhouse kernel principle graphic explanation] about the collaborative work of partitioning, indexing, marking and compressed data
随机推荐
FairyGUI复选框与进度条的组合使用
Lock wait timeout exceeded try restarting transaction
First use of dosbox
The service robots that have been hyped by capital and the Winter Olympics are not just a flash in the pan
Itext 7 生成PDF总结
NRF24L01 troubleshooting
Conditional probability
NovAtel 板卡OEM617D配置步骤记录
There is no red exclamation mark after SVN update
【rtklib】在rtk下使用抗差自适应卡尔曼滤波初步实践
What are the advantages of using SQL in Excel VBA
rtklib单点定位spp使用抗差估计遇到的问题及解决
(课设第一套)1-5 317号子任务 (100 分)(Dijkstra:重边自环)
SVN更新后不出现红色感叹号
Fairygui loop list
MySQL performance tuning - dirty page refresh
抗差估计在rtklib的pntpos函数(标准单点定位spp)中的c代码实现
FGUI工程打包发布&导入Unity&将UI显示出来的方式
idea中好用的快捷键
Expected value (EV)