Golang 如何定义一个接口类型的切片,它可以用来存储混合类型的数据,又如何自定义错误信息输出,以及如何定义变参函数,还有字符串多种拼接方式

前端之家收集整理的这篇文章主要介绍了Golang 如何定义一个接口类型的切片,它可以用来存储混合类型的数据,又如何自定义错误信息输出,以及如何定义变参函数,还有字符串多种拼接方式前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
package main @H_404_0@ @H_404_0@ import ( @H_404_0@ "errors" @H_404_0@ "fmt" @H_404_0@ ) @H_404_0@ @H_404_0@ func main() { @H_404_0@ //定义一个接口类型的切片,可以用来存储混合类型的数据 @H_404_0@ in := make([]interface{},0) @H_404_0@ in = append(in,2,"Go",8,"language",'a',false,"A",3.14) @H_404_0@ var x1 int = in[0].(int) @H_404_0@ var x2 string = in[1].(string) @H_404_0@ var x3 int = in[2].(int) @H_404_0@ var x4 string = in[3].(string) @H_404_0@ var x5 int32 = in[4].(int32) //注意它是int32类型而不是uint8类型 @H_404_0@ var x6 bool = in[5].(bool) @H_404_0@ var x7 string = in[6].(string) @H_404_0@ var x8 float64 = in[7].(float64) @H_404_0@ fmt.Println(" x1 = ",x1) @H_404_0@ fmt.Println(" x2 = ",x2) @H_404_0@ fmt.Println(" x3 = ",x3) @H_404_0@ fmt.Println(" x4 = ",x4) @H_404_0@ fmt.Println(" x5 = ",x5) @H_404_0@ fmt.Println(" x6 = ",x6) @H_404_0@ fmt.Println(" x7 = ",x7) @H_404_0@ fmt.Println(" x8 = ",x8) @H_404_0@ fmt.Println("in = ",in) @H_404_0@ fmt.Println("===================================================================================") @H_404_0@ data,err := concat(in...) @H_404_0@ if err == nil { @H_404_0@ fmt.Println("混合类型拼接字符串:",data) @H_404_0@ } else { @H_404_0@ fmt.Println("混合类型拼接字符串:",data) @H_404_0@ } @H_404_0@ } @H_404_0@ @H_404_0@ //变参函数concat有着不定数量和不确定类型的参数,把混合类型的多个值拼接成一个字符串 @H_404_0@ func concat(args ...interface{}) (ret string,err error) { @H_404_0@ for _,arg := range args { @H_404_0@ switch v := arg.(type) { @H_404_0@ case int: @H_404_0@ ret = ret + fmt.Sprintf("%v",v) @H_404_0@ case int32: @H_404_0@ ret = ret + fmt.Sprintf("%v",v) @H_404_0@ case int64: @H_404_0@ ret = ret + fmt.Sprintf("%v",v) @H_404_0@ case bool: @H_404_0@ ret = ret + fmt.Sprintf("%v",v) @H_404_0@ case string: @H_404_0@ ret = ret + fmt.Sprintf("%v",v) @H_404_0@ case float32: @H_404_0@ ret = ret + fmt.Sprintf("%v",v) @H_404_0@ case float64: @H_404_0@ ret = ret + fmt.Sprintf("%v",v) @H_404_0@

default:

err = errors.New(fmt.Sprintf("%v",v) + " data type has been not yet supported") // 自定义错误信息输出 @H_404_0@ return ret,err @H_404_0@ } @H_404_0@ } @H_404_0@ return ret,nil @H_404_0@

}

调试控制台输出以下信息:@H_404_0@

2017/06/30 17:00:45 server.go:73: Using API v1@H_404_0@ 2017/06/30 17:00:45 debugger.go:97: launching process with args: [/root/code/go/src/contoso.org/book/debug]@H_404_0@ API server listening at: 127.0.0.1:2345@H_404_0@ 2017/06/30 17:00:45 debugger.go:505: continuing@H_404_0@ x1 = 2@H_404_0@ x2 = Go@H_404_0@ x3 = 8@H_404_0@ x4 = language@H_404_0@ x5 = 97@H_404_0@ x6 = false@H_404_0@ x7 = A@H_404_0@ x8 = 3.14@H_404_0@ in = [2 Go 8 language 97 false A 3.14]@H_404_0@ ===================================================================================

混合类型拼接字符串: 2Go8language97falseA3.14

@H_404_0@

另外一个范例:@H_404_0@

package main@H_404_0@ @H_404_0@ import (@H_404_0@ "fmt"@H_404_0@ )@H_404_0@ @H_404_0@ func main() {@H_404_0@ //传入多个参数值直接调用变参函数,第1次传入7个参数值@H_404_0@ sum1 := sum(1,3,4,5,6,7)

//传入多个参数值直接调用变参函数,第2次传入10个参数值

sum2 := sum(1,7,9,10)@H_404_0@ fmt.Println("sum1 = ",sum1)@H_404_0@ fmt.Println("sum2 = ",sum2)@H_404_0@ //定义一个整数类型的切片params,并初始化它作为变参@H_404_0@ params := []int{1,10,11,12,13,14,15,16,17,18,19,20}@H_404_0@ sum3 := sum(params...)@H_404_0@ fmt.Println("sum3 = ",sum3)@H_404_0@ }@H_404_0@ @H_404_0@ //变参函数sum有着不定数量的参数@H_404_0@ func sum(args ...int) (ret int) {@H_404_0@ for _,arg := range args {@H_404_0@ ret += arg@H_404_0@ }@H_404_0@ return //另外一种格式:return ret@H_404_0@ }

调试控制台输出以下信息:

API server listening at: 127.0.0.1:2345@H_404_0@ 2017/06/30 17:38:52 debugger.go:505: continuing@H_404_0@ sum1 = 28@H_404_0@ sum2 = 55@H_404_0@ sum3 = 210

@H_404_0@

字符串的多种拼接方式范例:@H_404_0@

package main@H_404_0@ @H_404_0@ import (@H_404_0@ "bytes"@H_404_0@ "fmt"@H_404_0@ )@H_404_0@ @H_404_0@ func main() {@H_404_0@ //方法一:字符串拼接@H_404_0@ /*@H_404_0@ var str1 = "this is a testing string"@H_404_0@ a := []byte(str1) // 字符串转[]uint8类型@H_404_0@ b := []byte(",and this is another appended testing string")@H_404_0@ result := append(a,b...)@H_404_0@ fmt.Println(string(result))@H_404_0@ */@H_404_0@ //方法二:字符串拼接@H_404_0@ var str1 = "this is a testing string"@H_404_0@ var str2 = ",and this is another appended testing string"@H_404_0@ var str3 = str1 + str2@H_404_0@ fmt.Println("str3 = ",str3)@H_404_0@ @H_404_0@ //方法三:字符串拼接(这种方式最快)@H_404_0@ var str4 = "hello,nice to meeting you"@H_404_0@ var str5 = ",welcome to china again."@H_404_0@ var buffer bytes.Buffer //Buffer是一个实现了读写方法的可变大小的字节缓冲@H_404_0@ buffer.WriteString(str4)@H_404_0@ buffer.WriteString(str5)@H_404_0@ fmt.Println("字符串拼接结果: ",buffer.String())

//方法四:字符串混合值类型的拼接,直接使用NewBufferString创建缓存,直接使用另外一个NewBuffer创建缓存 str6 := "Falling" byte11 := []byte(" in ") //字符串转uint8类型的数组 byte12 := []byte{'L','o','v','e'} //uint8类的切片初始化 buf1 := bytes.NewBufferString(str6) buf2 := bytes.NewBuffer(byte11) buf3 := bytes.NewBuffer(byte12) fmt.Println("字符串拼接结果:",buf1.String()+buf2.String()+buf3.String()) }

原文链接:https://www.f2er.com/go/188314.html

猜你在找的Go相关文章