当前位置:网站首页>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
边栏推荐
猜你喜欢
随机推荐
SSD technical features
Special palindromes of daily practice of Blue Bridge Cup
There is no red exclamation mark after SVN update
Naive Bayesian theory derivation
By v$rman_ backup_ job_ Oracle "bug" caused by details
SVN更新后不出现红色感叹号
Derivation of logistic regression theory
341. Flatten nested list iterator
[Chongqing Guangdong education] Shandong University College Physics reference materials
[算法] 剑指offer2 golang 面试题2:二进制加法
Introduction to the daily practice column of the Blue Bridge Cup
Combination of fairygui check box and progress bar
KF UD分解之伪代码实现进阶篇【2】
NRF24L01 troubleshooting
Halcon knowledge: gray_ Tophat transform and bottom cap transform
(the first set of course design) 1-4 message passing interface (100 points) (simulation: thread)
FairyGUI增益BUFF数值改变的显示
FairyGUI复选框与进度条的组合使用
[leetcode622] design circular queue
FairyGUI循環列錶