使用空接口更改函数中的 arg

Change arg in function using empty interface

我正在使用 Golang,我有一个问题:

我可以写这个函数:

func newprint(a interface{}){
    switch a.(type){
    case int:
        fmt.println("this is integer")
    case string:
        fmt.println("this is string")
    case float:
        fmt.println("this is float")
}
}

我想使用一个使用空接口并根据其类型更改 arg 的函数。 例如:

如果 arg 的类型是int i想要向其添加 2 个单位
如果 arg 的类型是 float i 想要向其添加 5 个单位并且
如果 arg 的类型是字符串就打印它

要修改变量,将指向变量的指针传递给函数:

func newChange(a interface{}) {
    switch a := a.(type) {
    case *int:
        *a += 2
    case *float64:
        *a += 5.0
    }
}

这样称呼它:

a := 1
newChange(&a)

Run this program on the GoLang PlayGround.