当前位置:网站首页>Try the map and slice of the model version in go 1.18

Try the map and slice of the model version in go 1.18

2022-01-15 02:03:31 Robert_ Lu

Everyone has been paying attention to Go 1.18 Will support the message of the paradigm .

As Golang Built in type , Everyone is looking forward to it map and slice After supporting the paradigm , It can simplify a lot of judgment logic , such as Equal Logic and so on .

A few days ago ,Go The standard library of templates has been submitted , And you can try :

You can also read the corresponding code :https://cs.opensource.google/...

I don't say much nonsense , Let's see how to try the prototype version map and slice Well !

How to use Go 1.18?

Golang The official website links only 1.17 Version download , So how can we use 1.18 Version of Golang Well ?

On the Internet , Someone offered Golang 1.18 Version of Docker Mirror image seongwoohong/golang-nightly:1.18, And guarantee the maintenance to 1.18 Official release :

Then we can start one with the following command go1.18 The compiler environment of :

$ docker run --rm -it -v $PWD:/root/go-generics seongwoohong/golang-nightly:1.18 sh
# cd /root/go-generics/
~/go-generics #

Try maps

Flip down maps Code and test cases , Use the following code to demonstrate maps The function of :

package main

import (
    "fmt"
    "strconv"

    "golang.org/x/exp/maps" // v0.0.0-20211129234152-8a230f1f7d7a
)

func main() {
    var m1 = map[int]int{1: 2, 2: 4, 4: 8, 8: 16}
    var m2 = map[string]string{"1": "2", "2": "4", "4": "8", "8": "16"}
    var m3 = map[int]string{1: "2", 2: "4", 4: "8", 8: "16"}

    // Keys  Method returns map All keys for 
    //  If there is no paradigm , So for each map, All need to be written Keys Method , Only one... Is needed now 
    fmt.Printf("m1 Keys:\t%#v\n", maps.Keys(m1))
    fmt.Printf("m2 Kyes:\t%#v\n", maps.Keys(m2))

    // Values  Method returns map All values 
    fmt.Printf("m1 Values:\t%#v\n", maps.Values(m1))
    fmt.Printf("m2 Values:\t%#v\n", maps.Values(m2))

    //  Judge map Whether it is equal or not 
    fmt.Println("m1==m1?\t", maps.Equal(m1, m1))
    fmt.Println("m2==m2?\t", maps.Equal(m2, m2))
    //fmt.Println(maps.Equal(m1, m2))
    // map[int]int And map[string]string Can't compare 

    //  Judge map Whether it is equal or not ( Manually specify judgment logic )
    fmt.Println("m1==m3?\t", maps.EqualFunc(m1, m3, func(v1 int, v2 string) bool {
        return strconv.Itoa(v1) == v2
    }))

    //  Some more Clear、Clone、Copy、DeleteFunc, Are functions known by name 
}

Then compile and execute :

# go build -gcflags="-G=3 -lang=go1.18"
# ./m
m1 Keys:    []int{1, 2, 4, 8}
m2 Kyes:    []string{"8", "1", "2", "4"}
m1 Values:    []int{2, 4, 8, 16}
m2 Values:    []string{"16", "2", "4", "8"}
m1==m1?     true
m2==m2?     true
m1==m3?     true

Try slices

Again , Turn over slices Code and test cases , You can use the following code to demonstrate slices The function of :

package main

import (
    "fmt"
    "strconv"

    "golang.org/x/exp/slices" // v0.0.0-20211129234152-8a230f1f7d7a
)

func main() {
    var s1 = []int{1, 2, 3}
    var s2 = []string{"1", "2", "3"}

    //  Judge slice Whether it is equal or not 
    fmt.Println("s1==s1?\t", slices.Equal(s1, s1))
    fmt.Println("s2==s2?\t", slices.Equal(s2, s2))
    //fmt.Println(slices.Equal(s1, s2))
    //[]int And []string Can't compare 

    //  Judge slice Whether it is equal or not ( Manually specify judgment logic )
    fmt.Println("s1==s2?\t", slices.EqualFunc(s1, s2, func(v1 int, v2 string) bool {
        return strconv.Itoa(v1) == v2
    }))

    //  stay slice Find an element in 
    fmt.Printf("s1[%v]=%d\n", slices.Index(s1, 2), 2)
    fmt.Printf("s1[%v]=%d\n", slices.Index(s1, 999), 999)

    //  Some more Clear、Clone、Copy、DeleteFunc, Are functions known by name 
}

It can also be compiled and executed :

# go build -gcflags="-G=3 -lang=go1.18"
# ./m
s1==s1?     true
s2==s2?     true
s1==s2?     true
s1[1]=2
s1[-1]=999

summary

Compared with Java The paradigm of ,Golang It really achieves the runtime paradigm , Can improve some efficiency .

what's more , Like Wang Yin make complaints about Tucao before. ,Java The array of does not support stereotypes , This makes Java The type system is not so complete . At this point ,Golang Do better .

版权声明
本文为[Robert_ Lu]所创,转载请带上原文链接,感谢
https://chowdera.com/2021/12/202112122212497175.html

随机推荐