当前位置:网站首页>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
边栏推荐
- Containers and Devops: container based Devops delivery pipeline
- Unity3D基础入门之粒子系统(属性介绍+火焰粒子系统案例制作)
- 【无标题】
- FairyGUI增益BUFF數值改變的顯示
- Database course design: college educational administration management system (including code)
- KF UD分解之伪代码实现进阶篇【2】
- [899] ordered queue
- (课设第一套)1-4 消息传递接口 (100 分)(模拟:线程)
- Database table splitting strategy
- Unity3D摄像机,键盘控制前后左右上下移动,鼠标控制旋转、放缩
猜你喜欢
[算法] 剑指offer2 golang 面试题8:和大于或等于k的最短子数组
Combination of fairygui check box and progress bar
Unity3D基础入门之粒子系统(属性介绍+火焰粒子系统案例制作)
Fairygui joystick
Unity3d makes the registration login interface and realizes the scene jump
The service robots that have been hyped by capital and the Winter Olympics are not just a flash in the pan
Easy to use shortcut keys in idea
Halcon knowledge: gray_ Tophat transform and bottom cap transform
Latex learning
Mixed use of fairygui button dynamics
随机推荐
(1) Introduction Guide to R language - the first step of data analysis
dosbox第一次使用
[算法] 剑指offer2 golang 面试题13:二维子矩阵的数字之和
FairyGUI复选框与进度条的组合使用
Esp8266 connect onenet (old mqtt mode)
Excel导入,导出功能实现
Combination of fairygui check box and progress bar
[offer9] implement queues with two stacks
(core focus of software engineering review) Chapter V detailed design exercises
FairyGUI摇杆
[Yu Yue education] guide business reference materials of Wuxi Vocational and Technical College of Commerce
Meanings and differences of PV, UV, IP, VV, CV
[offer9]用两个栈实现队列
Itext 7 生成PDF总结
Design and implementation of general interface open platform - (39) simple and crude implementation of API services
InnoDB dirty page refresh mechanism checkpoint in MySQL
【GNSS数据处理】赫尔默特(helmert)方差分量估计解析及代码实现
【RTKLIB 2.4.3 b34 】版本更新简介一
VLSM variable length subnet mask partition tips
[算法] 剑指offer2 golang 面试题4:只出现一次的数字