企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持知识库和私有化部署方案 广告
> go语言github项目:https://github.com/minibear2333/how_to_code ![](https://coding3min.oss-accelerate.aliyuncs.com/coding3min/2020-04-14-123855.jpg) [TOC] ### 常量 常量就是**不可变的变量**,定义方式 ```go const identifier [type] = value ``` 约定常量全大写表示 ```go const A int = 1 const B = 1 const C, D, E = 1, 1, 1 ``` 一般常量被用于枚举 ```go const ( Success = 0 UnKonw = 1 Error = 2 ) ``` 不过要枚举还是用 `go` 自带的特殊常量好一点,这种特殊被认为是可以被编译器修改的常量 ```go //const 出现时被重置为0,每出现一次自动加1 const ( F = iota G = iota H = iota ) ``` F、G、H 值为0,1,2 当然可以简写成这样,效果是一样的。 ```go const ( I = iota J K ) ``` ### 类型转换 没有什么好说的,和其他语言相似,类型转换都是类型+变量的形式,如下。 ```go var aInt int = 17 // 一般用这种方式强制转 fmt.Printf("转float64 %f \n", float64(aInt)) fmt.Printf("转string %v \n", strconv.Itoa(aInt)) fmt.Printf("转float64 %f \n", float64(aInt))[] ``` 输出 ``` 转float64 17.000000 转string 17 转float64 17.000000 ``` 各种类型转字符串 ```go resString := fmt.Sprintf("%d %v %v", 1, "coding3min", true) fmt.Println(resString) ``` 输出 ```go 1 coding3min true ``` `string` 和 `bytes` 的互相转换 ```go // string to bytes resBytes := []byte("asdfasdf") // bytes to string resString = string(resBytes) fmt.Println(resString) ``` 输出 ``` asdfasdf ``` 这一节学完,你觉得最有用的部分是什么呢?