4

Hello everyone, I am fried fish.

When you look at the Go1.18 generic code, you don't know if you noticed a new keyword any.

Examples are as follows:

func Print[T any](s []T) {}

I haven't specifically mentioned it before, but does anyone think that this keyword is exclusive to generic code?

Actually not... In this new Go1.18 update, any appears as a new keyword. any has a real body, which is essentially the alias of interface{}:

type any = interface{}

That is, in regular code, you can also use it directly:

func f(a any) {
    switch a.(type) {
    case int:
        fmt.Println("进脑子煎鱼了")
    case float64:
        fmt.Println("煎鱼进脑子了")
    case string:
        fmt.Println("脑子进煎鱼了")
    }
}

func main() {
    f(2)
    f(3.1415)
    f("煎鱼好!")
}

From the perspective of usage, the new keyword any is much more convenient than interface{}. After all, it is faster with fewer words. In fact, it is also based on the existing rune type.

The comparison after adding new keywords is as follows:

Long statementShort statement
func fT interface{} []Tfunc fT any []T
func f(a interface{})func f(a any)
var a interface{}var a any

After we understand his convenience, in terms of code consistency and readability, there are some problems, which will cause certain doubts.

Therefore, someone put forward the requirement of " all: rewrite interface{} to any " two days ago, intending to rewrite all the internal codes.

You might think it is a manual modification of human flesh? That's certainly not the case. Go officially initiated CL for batch modification.

In our daily projects, we can also directly borrow the Go toolchain to implement replacements just like them.

as follows:

gofmt -w -r 'interface{} -> any' ./...

When hearing this news, my friend Xianyu was shocked and wondered whether interface{} will become history and be completely replaced by the new keyword any?

Obviously, the answer is no. Because Go1 has compatibility guarantee , it will definitely not be deleted at this stage. However, there will be a phenomenon in the future, that is, in our Go project, some people use any and some people use interface{}, which will hurt the readability of the code.

However, we can also learn from the Go official, and use the gofmt tool in the linter process to forcefully replace all interface{} with any to achieve code consistency.

This change aesthetics 161ca99dcdb121, what do you think of this? Do you have any aliases and wish to have aliases, welcomes everyone to leave a message and exchange in the comment area :)

If you have any questions, welcome feedback and exchanges in the comment area. The best relationship between . Your likes is the biggest motivation for the creation of fried fish

The article is continuously updated, and you can read it on search [My brain is fried fish]. This article 161ca99dcdb255 GitHub github.com/eddycjy/blog has been included. For learning Go language, you can see Go learning map and route . Welcome to Star reminder.

煎鱼
8.4k 声望12.8k 粉丝