GO语言练习:不定参数函数-爱分乐赚

1、代码

2、运行


1、代码

package main
import "fmt"

func MyPrintf(args ...interface{}){
    for _, arg := range args {
        switch arg.(type) {
        case int :
            fmt.Println(""", arg, """, "is an int value.")
        case string :
            fmt.Println(""", arg, """, "is a string value.")
        case int64 :
            fmt.Println(""", arg, """, "is an int64 value.")
            default :
            fmt.Println(""", arg, """, "is an unknown type.")
        }
    }
}

func main(){
    var v1 int = 1
    var v2 int64 = 234
    var v3 string = "hello"
    var v4 float32 = 1.123
    MyPrintf(v1, v2, v3, v4)
}

2、运行

$ go run myprintf.go
" 1 " is an int value.
" 234 " is an int64 value.
" hello " is a string value.
" 1.123 " is an unknown type.

 

本文转自:https://www.cnblogs.com/fengbohello/p/4620523.html

----------------

微信扫一扫,分享到朋友圈

GO语言练习:不定参数函数-爱分乐赚
返回顶部

显示

忘记密码?

显示

显示

获取验证码

Close