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@}
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)
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