1 Star 0 Fork 0

encircles/learnGo

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
demo12.go 1.33 KB
一键复制 编辑 原始数据 按行查看 历史
encircles 提交于 2019-03-10 22:22 +08:00 . 190310 1
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
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/encircles/learnGo.git
git@gitee.com:encircles/learnGo.git
encircles
learnGo
learnGo
master

搜索帮助