了解和记忆go中的Itoa、Atoi和iota

914 阅读1分钟

刚开始不理解为什么 strcov 包中会有ItoaAtoi看着难以记忆的函数名,而且和iota混淆,原来这些命名都是有原因的,了解后更方便记忆

1. strcov包中的 ItoaAtoi

  • Itoa (用于将整数转换为字符串)

来源:早期c语言中没有string类型而是用字符数组array表示字符串,所以 Itoa 是缩写于Int to Array

  • Atoi(用于将字符串转换为整数)

来源:Array to Int

2. iota

iota是golang语言的常量计数器,只能在常量的表达式中使用。

// 基本用法
// iota只能在常量的表达式中使用。

fmt.Println(iota)  
编译错误: undefined: iota


//每次 const 出现时,都会让 iota 初始化为0.

const a = iota // a=0  
const (  
  b = iota     //b=0  
  c            //c=1  
)

来源:iota [ aɪˈəʊtə ] n. 极微小;希腊语的第九个字母