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

前端之家收集整理的这篇文章主要介绍了Golang 如何定义一个接口类型的切片,它可以用来存储混合类型的数据,又如何自定义错误信息输出,以及如何定义变参函数,还有字符串多种拼接方式前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
package main

import (
"errors"
"fmt"
)

func main() {
//定义一个接口类型的切片,可以用来存储混合类型的数据
in := make([]interface{},0)
in = append(in,2,"Go",8,"language",'a',false,"A",3.14)
var x1 int = in[0].(int)
var x2 string = in[1].(string)
var x3 int = in[2].(int)
var x4 string = in[3].(string)
var x5 int32 = in[4].(int32) //注意它是int32类型而不是uint8类型
var x6 bool = in[5].(bool)
var x7 string = in[6].(string)
var x8 float64 = in[7].(float64)
fmt.Println(" x1 = ",x1)
fmt.Println(" x2 = ",x2)
fmt.Println(" x3 = ",x3)
fmt.Println(" x4 = ",x4)
fmt.Println(" x5 = ",x5)
fmt.Println(" x6 = ",x6)
fmt.Println(" x7 = ",x7)
fmt.Println(" x8 = ",x8)
fmt.Println("in = ",in)
fmt.Println("===================================================================================")
data,err := concat(in...)
if err == nil {
fmt.Println("混合类型拼接字符串:",data)
} else {
fmt.Println("混合类型拼接字符串:",data)
}
}

//变参函数concat有着不定数量和不确定类型的参数,把混合类型的多个值拼接成一个字符串
func concat(args ...interface{}) (ret string,err error) {
for _,arg := range args {
switch v := arg.(type) {
case int:
ret = ret + fmt.Sprintf("%v",v)
case int32:
ret = ret + fmt.Sprintf("%v",v)
case int64:
ret = ret + fmt.Sprintf("%v",v)
case bool:
ret = ret + fmt.Sprintf("%v",v)
case string:
ret = ret + fmt.Sprintf("%v",v)
case float32:
ret = ret + fmt.Sprintf("%v",v)
case float64:
ret = ret + fmt.Sprintf("%v",v)

default:

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

}

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

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

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


另外一个范例:

package main

import (
"fmt"
)

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

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

sum2 := sum(1,7,9,10)
fmt.Println("sum1 = ",sum1)
fmt.Println("sum2 = ",sum2)
//定义一个整数类型的切片params,并初始化它作为变参
params := []int{1,10,11,12,13,14,15,16,17,18,19,20}
sum3 := sum(params...)
fmt.Println("sum3 = ",sum3)
}

//变参函数sum有着不定数量的参数
func sum(args ...int) (ret int) {
for _,arg := range args {
ret += arg
}
return //另外一种格式:return ret
}

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

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


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

package main

import (
"bytes"
"fmt"
)

func main() {
//方法一:字符串拼接
/*
var str1 = "this is a testing string"
a := []byte(str1) // 字符串转[]uint8类型
b := []byte(",and this is another appended testing string")
result := append(a,b...)
fmt.Println(string(result))
*/
//方法二:字符串拼接
var str1 = "this is a testing string"
var str2 = ",and this is another appended testing string"
var str3 = str1 + str2
fmt.Println("str3 = ",str3)

//方法三:字符串拼接(这种方式最快)
var str4 = "hello,nice to meeting you"
var str5 = ",welcome to china again."
var buffer bytes.Buffer //Buffer是一个实现了读写方法的可变大小的字节缓冲
buffer.WriteString(str4)
buffer.WriteString(str5)
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相关文章