代码拉取完成,页面将自动刷新
package main
import (
"fmt"
"strconv"
)
var container = []string{"zero", "one", "two"}
func main() {
container := map[int]string{0: "zero", 1: "one", 2: "two"}
// 方式一
_, ok1 := interface{}(container).([]string)
_, ok2 := interface{}(container).(map[int]string)
if !(ok1 || ok2) {
fmt.Printf("Error: unsupported container type: %T\n", container)
return
}
fmt.Printf("The element is %q. (container type: %T)\n", container[1], container)
// 方式2
elem, err := getElement(container)
if err != nil {
fmt.Printf("Error: %s\n", err)
return
}
fmt.Printf("The element is %q. (container type: %T)\n", elem, container)
test()
}
func test() {
var srcInt = int16(-255)
dstInt := int8(srcInt)
var t1 int8 = 1
var str string
for i := 1; i < 3; i++ {
str = string(i)
fmt.Printf("%q\n", str)
}
byte1, err := strconv.Atoi("100")
if err != nil {
fmt.Printf("convert error %s\n", err)
}
test := "你好"
var b2 []byte = []byte(test + "n")
fmt.Println(b2)
fmt.Printf("%T\n", byte1)
fmt.Printf("%T\n", b2)
fmt.Printf("%q\n", dstInt)
fmt.Printf("%q\n", t1)
}
func getElement(containerI interface{}) (elem string, err error) {
switch t := containerI.(type) {
case []string:
elem = t[1]
case map[int]string:
elem = t[1]
default:
err = fmt.Errorf("unsupported container type: %T", containerI)
return
}
return
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。