当前位置:网站首页>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
边栏推荐
- Derivation of logistic regression theory
- Design and implementation of general interface open platform - (39) simple and crude implementation of API services
- [算法] 剑指offer2 golang 面试题2:二进制加法
- What are the advantages of using SQL in Excel VBA
- Guided package method in idea
- [Chongqing Guangdong education] Shandong University College Physics reference materials
- MySQL error warning: a long semaphore wait
- [offer78]合并多个有序链表
- Office prompts that your license is not genuine pop-up box solution
- (core focus of software engineering review) Chapter V detailed design exercises
猜你喜欢
(1) Introduction Guide to R language - the first step of data analysis
[算法] 剑指offer2 golang 面试题6:排序数组中的两个数字之和
Mixed use of fairygui button dynamics
Teach you to release a DeNO module hand in hand
KF UD分解之UD分解基础篇【1】
基本Dos命令
PR 2021 quick start tutorial, first understanding the Premiere Pro working interface
[算法] 剑指offer2 golang 面试题10:和为k的子数组
單片機藍牙無線燒錄
Particle system for introduction to unity3d Foundation (attribute introduction + case production of flame particle system)
随机推荐
Conditional probability
The service robots that have been hyped by capital and the Winter Olympics are not just a flash in the pan
[算法] 剑指offer2 golang 面试题5:单词长度的最大乘积
【无标题】
Unity scene jump and exit
音乐播放(Toggle && PlayerPrefs)
ORA-02030: can only select from fixed tables/views
FairyGUI复选框与进度条的组合使用
服务未正常关闭导致端口被占用
Compile GDAL source code with nmake (win10, vs2022)
抗差估计在rtklib的pntpos函数(标准单点定位spp)中的c代码实现
SVN更新后不出现红色感叹号
Vulnhub target: hacknos_ PLAYER V1.1
單片機藍牙無線燒錄
Liste des boucles de l'interface graphique de défaillance
Latex learning
FairyGUI增益BUFF數值改變的顯示
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
【GNSS】抗差估计(稳健估计)原理及程序实现
KF UD分解之UD分解基础篇【1】