You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

README_ZH.md 19 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733
  1. 本包提供了 Go 语言中读写 INI 文件的功能。
  2. ## 功能特性
  3. - 支持覆盖加载多个数据源(`[]byte`、文件和 `io.ReadCloser`)
  4. - 支持递归读取键值
  5. - 支持读取父子分区
  6. - 支持读取自增键名
  7. - 支持读取多行的键值
  8. - 支持大量辅助方法
  9. - 支持在读取时直接转换为 Go 语言类型
  10. - 支持读取和 **写入** 分区和键的注释
  11. - 轻松操作分区、键值和注释
  12. - 在保存文件时分区和键值会保持原有的顺序
  13. ## 下载安装
  14. 使用一个特定版本:
  15. go get gopkg.in/ini.v1
  16. 使用最新版:
  17. go get github.com/go-ini/ini
  18. 如需更新请添加 `-u` 选项。
  19. ### 测试安装
  20. 如果您想要在自己的机器上运行测试,请使用 `-t` 标记:
  21. go get -t gopkg.in/ini.v1
  22. 如需更新请添加 `-u` 选项。
  23. ## 开始使用
  24. ### 从数据源加载
  25. 一个 **数据源** 可以是 `[]byte` 类型的原始数据,`string` 类型的文件路径或 `io.ReadCloser`。您可以加载 **任意多个** 数据源。如果您传递其它类型的数据源,则会直接返回错误。
  26. ```go
  27. cfg, err := ini.Load([]byte("raw data"), "filename", ioutil.NopCloser(bytes.NewReader([]byte("some other data"))))
  28. ```
  29. 或者从一个空白的文件开始:
  30. ```go
  31. cfg := ini.Empty()
  32. ```
  33. 当您在一开始无法决定需要加载哪些数据源时,仍可以使用 **Append()** 在需要的时候加载它们。
  34. ```go
  35. err := cfg.Append("other file", []byte("other raw data"))
  36. ```
  37. 当您想要加载一系列文件,但是不能够确定其中哪些文件是不存在的,可以通过调用函数 `LooseLoad` 来忽略它们(`Load` 会因为文件不存在而返回错误):
  38. ```go
  39. cfg, err := ini.LooseLoad("filename", "filename_404")
  40. ```
  41. 更牛逼的是,当那些之前不存在的文件在重新调用 `Reload` 方法的时候突然出现了,那么它们会被正常加载。
  42. #### 忽略键名的大小写
  43. 有时候分区和键的名称大小写混合非常烦人,这个时候就可以通过 `InsensitiveLoad` 将所有分区和键名在读取里强制转换为小写:
  44. ```go
  45. cfg, err := ini.InsensitiveLoad("filename")
  46. //...
  47. // sec1 和 sec2 指向同一个分区对象
  48. sec1, err := cfg.GetSection("Section")
  49. sec2, err := cfg.GetSection("SecTIOn")
  50. // key1 和 key2 指向同一个键对象
  51. key1, err := sec1.GetKey("Key")
  52. key2, err := sec2.GetKey("KeY")
  53. ```
  54. #### 类似 MySQL 配置中的布尔值键
  55. MySQL 的配置文件中会出现没有具体值的布尔类型的键:
  56. ```ini
  57. [mysqld]
  58. ...
  59. skip-host-cache
  60. skip-name-resolve
  61. ```
  62. 默认情况下这被认为是缺失值而无法完成解析,但可以通过高级的加载选项对它们进行处理:
  63. ```go
  64. cfg, err := LoadSources(LoadOptions{AllowBooleanKeys: true}, "my.cnf"))
  65. ```
  66. 这些键的值永远为 `true`,且在保存到文件时也只会输出键名。
  67. 如果您想要通过程序来生成此类键,则可以使用 `NewBooleanKey`:
  68. ```go
  69. key, err := sec.NewBooleanKey("skip-host-cache")
  70. ```
  71. #### 关于注释
  72. 下述几种情况的内容将被视为注释:
  73. 1. 所有以 `#` 或 `;` 开头的行
  74. 2. 所有在 `#` 或 `;` 之后的内容
  75. 3. 分区标签后的文字 (即 `[分区名]` 之后的内容)
  76. 如果你希望使用包含 `#` 或 `;` 的值,请使用 ``` ` ``` 或 ``` """ ``` 进行包覆。
  77. 除此之外,您还可以通过 `LoadOptions` 完全忽略行内注释:
  78. ```go
  79. cfg, err := LoadSources(LoadOptions{IgnoreInlineComment: true}, "app.ini"))
  80. ```
  81. ### 操作分区(Section)
  82. 获取指定分区:
  83. ```go
  84. section, err := cfg.GetSection("section name")
  85. ```
  86. 如果您想要获取默认分区,则可以用空字符串代替分区名:
  87. ```go
  88. section, err := cfg.GetSection("")
  89. ```
  90. 当您非常确定某个分区是存在的,可以使用以下简便方法:
  91. ```go
  92. section := cfg.Section("section name")
  93. ```
  94. 如果不小心判断错了,要获取的分区其实是不存在的,那会发生什么呢?没事的,它会自动创建并返回一个对应的分区对象给您。
  95. 创建一个分区:
  96. ```go
  97. err := cfg.NewSection("new section")
  98. ```
  99. 获取所有分区对象或名称:
  100. ```go
  101. sections := cfg.Sections()
  102. names := cfg.SectionStrings()
  103. ```
  104. ### 操作键(Key)
  105. 获取某个分区下的键:
  106. ```go
  107. key, err := cfg.Section("").GetKey("key name")
  108. ```
  109. 和分区一样,您也可以直接获取键而忽略错误处理:
  110. ```go
  111. key := cfg.Section("").Key("key name")
  112. ```
  113. 判断某个键是否存在:
  114. ```go
  115. yes := cfg.Section("").HasKey("key name")
  116. ```
  117. 创建一个新的键:
  118. ```go
  119. err := cfg.Section("").NewKey("name", "value")
  120. ```
  121. 获取分区下的所有键或键名:
  122. ```go
  123. keys := cfg.Section("").Keys()
  124. names := cfg.Section("").KeyStrings()
  125. ```
  126. 获取分区下的所有键值对的克隆:
  127. ```go
  128. hash := cfg.Section("").KeysHash()
  129. ```
  130. ### 操作键值(Value)
  131. 获取一个类型为字符串(string)的值:
  132. ```go
  133. val := cfg.Section("").Key("key name").String()
  134. ```
  135. 获取值的同时通过自定义函数进行处理验证:
  136. ```go
  137. val := cfg.Section("").Key("key name").Validate(func(in string) string {
  138. if len(in) == 0 {
  139. return "default"
  140. }
  141. return in
  142. })
  143. ```
  144. 如果您不需要任何对值的自动转变功能(例如递归读取),可以直接获取原值(这种方式性能最佳):
  145. ```go
  146. val := cfg.Section("").Key("key name").Value()
  147. ```
  148. 判断某个原值是否存在:
  149. ```go
  150. yes := cfg.Section("").HasValue("test value")
  151. ```
  152. 获取其它类型的值:
  153. ```go
  154. // 布尔值的规则:
  155. // true 当值为:1, t, T, TRUE, true, True, YES, yes, Yes, y, ON, on, On
  156. // false 当值为:0, f, F, FALSE, false, False, NO, no, No, n, OFF, off, Off
  157. v, err = cfg.Section("").Key("BOOL").Bool()
  158. v, err = cfg.Section("").Key("FLOAT64").Float64()
  159. v, err = cfg.Section("").Key("INT").Int()
  160. v, err = cfg.Section("").Key("INT64").Int64()
  161. v, err = cfg.Section("").Key("UINT").Uint()
  162. v, err = cfg.Section("").Key("UINT64").Uint64()
  163. v, err = cfg.Section("").Key("TIME").TimeFormat(time.RFC3339)
  164. v, err = cfg.Section("").Key("TIME").Time() // RFC3339
  165. v = cfg.Section("").Key("BOOL").MustBool()
  166. v = cfg.Section("").Key("FLOAT64").MustFloat64()
  167. v = cfg.Section("").Key("INT").MustInt()
  168. v = cfg.Section("").Key("INT64").MustInt64()
  169. v = cfg.Section("").Key("UINT").MustUint()
  170. v = cfg.Section("").Key("UINT64").MustUint64()
  171. v = cfg.Section("").Key("TIME").MustTimeFormat(time.RFC3339)
  172. v = cfg.Section("").Key("TIME").MustTime() // RFC3339
  173. // 由 Must 开头的方法名允许接收一个相同类型的参数来作为默认值,
  174. // 当键不存在或者转换失败时,则会直接返回该默认值。
  175. // 但是,MustString 方法必须传递一个默认值。
  176. v = cfg.Seciont("").Key("String").MustString("default")
  177. v = cfg.Section("").Key("BOOL").MustBool(true)
  178. v = cfg.Section("").Key("FLOAT64").MustFloat64(1.25)
  179. v = cfg.Section("").Key("INT").MustInt(10)
  180. v = cfg.Section("").Key("INT64").MustInt64(99)
  181. v = cfg.Section("").Key("UINT").MustUint(3)
  182. v = cfg.Section("").Key("UINT64").MustUint64(6)
  183. v = cfg.Section("").Key("TIME").MustTimeFormat(time.RFC3339, time.Now())
  184. v = cfg.Section("").Key("TIME").MustTime(time.Now()) // RFC3339
  185. ```
  186. 如果我的值有好多行怎么办?
  187. ```ini
  188. [advance]
  189. ADDRESS = """404 road,
  190. NotFound, State, 5000
  191. Earth"""
  192. ```
  193. 嗯哼?小 case!
  194. ```go
  195. cfg.Section("advance").Key("ADDRESS").String()
  196. /* --- start ---
  197. 404 road,
  198. NotFound, State, 5000
  199. Earth
  200. ------ end --- */
  201. ```
  202. 赞爆了!那要是我属于一行的内容写不下想要写到第二行怎么办?
  203. ```ini
  204. [advance]
  205. two_lines = how about \
  206. continuation lines?
  207. lots_of_lines = 1 \
  208. 2 \
  209. 3 \
  210. 4
  211. ```
  212. 简直是小菜一碟!
  213. ```go
  214. cfg.Section("advance").Key("two_lines").String() // how about continuation lines?
  215. cfg.Section("advance").Key("lots_of_lines").String() // 1 2 3 4
  216. ```
  217. 可是我有时候觉得两行连在一起特别没劲,怎么才能不自动连接两行呢?
  218. ```go
  219. cfg, err := ini.LoadSources(ini.LoadOptions{
  220. IgnoreContinuation: true,
  221. }, "filename")
  222. ```
  223. 哇靠给力啊!
  224. 需要注意的是,值两侧的单引号会被自动剔除:
  225. ```ini
  226. foo = "some value" // foo: some value
  227. bar = 'some value' // bar: some value
  228. ```
  229. 这就是全部了?哈哈,当然不是。
  230. #### 操作键值的辅助方法
  231. 获取键值时设定候选值:
  232. ```go
  233. v = cfg.Section("").Key("STRING").In("default", []string{"str", "arr", "types"})
  234. v = cfg.Section("").Key("FLOAT64").InFloat64(1.1, []float64{1.25, 2.5, 3.75})
  235. v = cfg.Section("").Key("INT").InInt(5, []int{10, 20, 30})
  236. v = cfg.Section("").Key("INT64").InInt64(10, []int64{10, 20, 30})
  237. v = cfg.Section("").Key("UINT").InUint(4, []int{3, 6, 9})
  238. v = cfg.Section("").Key("UINT64").InUint64(8, []int64{3, 6, 9})
  239. v = cfg.Section("").Key("TIME").InTimeFormat(time.RFC3339, time.Now(), []time.Time{time1, time2, time3})
  240. v = cfg.Section("").Key("TIME").InTime(time.Now(), []time.Time{time1, time2, time3}) // RFC3339
  241. ```
  242. 如果获取到的值不是候选值的任意一个,则会返回默认值,而默认值不需要是候选值中的一员。
  243. 验证获取的值是否在指定范围内:
  244. ```go
  245. vals = cfg.Section("").Key("FLOAT64").RangeFloat64(0.0, 1.1, 2.2)
  246. vals = cfg.Section("").Key("INT").RangeInt(0, 10, 20)
  247. vals = cfg.Section("").Key("INT64").RangeInt64(0, 10, 20)
  248. vals = cfg.Section("").Key("UINT").RangeUint(0, 3, 9)
  249. vals = cfg.Section("").Key("UINT64").RangeUint64(0, 3, 9)
  250. vals = cfg.Section("").Key("TIME").RangeTimeFormat(time.RFC3339, time.Now(), minTime, maxTime)
  251. vals = cfg.Section("").Key("TIME").RangeTime(time.Now(), minTime, maxTime) // RFC3339
  252. ```
  253. ##### 自动分割键值到切片(slice)
  254. 当存在无效输入时,使用零值代替:
  255. ```go
  256. // Input: 1.1, 2.2, 3.3, 4.4 -> [1.1 2.2 3.3 4.4]
  257. // Input: how, 2.2, are, you -> [0.0 2.2 0.0 0.0]
  258. vals = cfg.Section("").Key("STRINGS").Strings(",")
  259. vals = cfg.Section("").Key("FLOAT64S").Float64s(",")
  260. vals = cfg.Section("").Key("INTS").Ints(",")
  261. vals = cfg.Section("").Key("INT64S").Int64s(",")
  262. vals = cfg.Section("").Key("UINTS").Uints(",")
  263. vals = cfg.Section("").Key("UINT64S").Uint64s(",")
  264. vals = cfg.Section("").Key("TIMES").Times(",")
  265. ```
  266. 从结果切片中剔除无效输入:
  267. ```go
  268. // Input: 1.1, 2.2, 3.3, 4.4 -> [1.1 2.2 3.3 4.4]
  269. // Input: how, 2.2, are, you -> [2.2]
  270. vals = cfg.Section("").Key("FLOAT64S").ValidFloat64s(",")
  271. vals = cfg.Section("").Key("INTS").ValidInts(",")
  272. vals = cfg.Section("").Key("INT64S").ValidInt64s(",")
  273. vals = cfg.Section("").Key("UINTS").ValidUints(",")
  274. vals = cfg.Section("").Key("UINT64S").ValidUint64s(",")
  275. vals = cfg.Section("").Key("TIMES").ValidTimes(",")
  276. ```
  277. 当存在无效输入时,直接返回错误:
  278. ```go
  279. // Input: 1.1, 2.2, 3.3, 4.4 -> [1.1 2.2 3.3 4.4]
  280. // Input: how, 2.2, are, you -> error
  281. vals = cfg.Section("").Key("FLOAT64S").StrictFloat64s(",")
  282. vals = cfg.Section("").Key("INTS").StrictInts(",")
  283. vals = cfg.Section("").Key("INT64S").StrictInt64s(",")
  284. vals = cfg.Section("").Key("UINTS").StrictUints(",")
  285. vals = cfg.Section("").Key("UINT64S").StrictUint64s(",")
  286. vals = cfg.Section("").Key("TIMES").StrictTimes(",")
  287. ```
  288. ### 保存配置
  289. 终于到了这个时刻,是时候保存一下配置了。
  290. 比较原始的做法是输出配置到某个文件:
  291. ```go
  292. // ...
  293. err = cfg.SaveTo("my.ini")
  294. err = cfg.SaveToIndent("my.ini", "\t")
  295. ```
  296. 另一个比较高级的做法是写入到任何实现 `io.Writer` 接口的对象中:
  297. ```go
  298. // ...
  299. cfg.WriteTo(writer)
  300. cfg.WriteToIndent(writer, "\t")
  301. ```
  302. 默认情况下,空格将被用于对齐键值之间的等号以美化输出结果,以下代码可以禁用该功能:
  303. ```go
  304. ini.PrettyFormat = false
  305. ```
  306. ## 高级用法
  307. ### 递归读取键值
  308. 在获取所有键值的过程中,特殊语法 `%(<name>)s` 会被应用,其中 `<name>` 可以是相同分区或者默认分区下的键名。字符串 `%(<name>)s` 会被相应的键值所替代,如果指定的键不存在,则会用空字符串替代。您可以最多使用 99 层的递归嵌套。
  309. ```ini
  310. NAME = ini
  311. [author]
  312. NAME = Unknwon
  313. GITHUB = https://github.com/%(NAME)s
  314. [package]
  315. FULL_NAME = github.com/go-ini/%(NAME)s
  316. ```
  317. ```go
  318. cfg.Section("author").Key("GITHUB").String() // https://github.com/Unknwon
  319. cfg.Section("package").Key("FULL_NAME").String() // github.com/go-ini/ini
  320. ```
  321. ### 读取父子分区
  322. 您可以在分区名称中使用 `.` 来表示两个或多个分区之间的父子关系。如果某个键在子分区中不存在,则会去它的父分区中再次寻找,直到没有父分区为止。
  323. ```ini
  324. NAME = ini
  325. VERSION = v1
  326. IMPORT_PATH = gopkg.in/%(NAME)s.%(VERSION)s
  327. [package]
  328. CLONE_URL = https://%(IMPORT_PATH)s
  329. [package.sub]
  330. ```
  331. ```go
  332. cfg.Section("package.sub").Key("CLONE_URL").String() // https://gopkg.in/ini.v1
  333. ```
  334. #### 获取上级父分区下的所有键名
  335. ```go
  336. cfg.Section("package.sub").ParentKeys() // ["CLONE_URL"]
  337. ```
  338. ### 无法解析的分区
  339. 如果遇到一些比较特殊的分区,它们不包含常见的键值对,而是没有固定格式的纯文本,则可以使用 `LoadOptions.UnparsableSections` 进行处理:
  340. ```go
  341. cfg, err := LoadSources(LoadOptions{UnparseableSections: []string{"COMMENTS"}}, `[COMMENTS]
  342. <1><L.Slide#2> This slide has the fuel listed in the wrong units <e.1>`))
  343. body := cfg.Section("COMMENTS").Body()
  344. /* --- start ---
  345. <1><L.Slide#2> This slide has the fuel listed in the wrong units <e.1>
  346. ------ end --- */
  347. ```
  348. ### 读取自增键名
  349. 如果数据源中的键名为 `-`,则认为该键使用了自增键名的特殊语法。计数器从 1 开始,并且分区之间是相互独立的。
  350. ```ini
  351. [features]
  352. -: Support read/write comments of keys and sections
  353. -: Support auto-increment of key names
  354. -: Support load multiple files to overwrite key values
  355. ```
  356. ```go
  357. cfg.Section("features").KeyStrings() // []{"#1", "#2", "#3"}
  358. ```
  359. ### 映射到结构
  360. 想要使用更加面向对象的方式玩转 INI 吗?好主意。
  361. ```ini
  362. Name = Unknwon
  363. age = 21
  364. Male = true
  365. Born = 1993-01-01T20:17:05Z
  366. [Note]
  367. Content = Hi is a good man!
  368. Cities = HangZhou, Boston
  369. ```
  370. ```go
  371. type Note struct {
  372. Content string
  373. Cities []string
  374. }
  375. type Person struct {
  376. Name string
  377. Age int `ini:"age"`
  378. Male bool
  379. Born time.Time
  380. Note
  381. Created time.Time `ini:"-"`
  382. }
  383. func main() {
  384. cfg, err := ini.Load("path/to/ini")
  385. // ...
  386. p := new(Person)
  387. err = cfg.MapTo(p)
  388. // ...
  389. // 一切竟可以如此的简单。
  390. err = ini.MapTo(p, "path/to/ini")
  391. // ...
  392. // 嗯哼?只需要映射一个分区吗?
  393. n := new(Note)
  394. err = cfg.Section("Note").MapTo(n)
  395. // ...
  396. }
  397. ```
  398. 结构的字段怎么设置默认值呢?很简单,只要在映射之前对指定字段进行赋值就可以了。如果键未找到或者类型错误,该值不会发生改变。
  399. ```go
  400. // ...
  401. p := &Person{
  402. Name: "Joe",
  403. }
  404. // ...
  405. ```
  406. 这样玩 INI 真的好酷啊!然而,如果不能还给我原来的配置文件,有什么卵用?
  407. ### 从结构反射
  408. 可是,我有说不能吗?
  409. ```go
  410. type Embeded struct {
  411. Dates []time.Time `delim:"|"`
  412. Places []string `ini:"places,omitempty"`
  413. None []int `ini:",omitempty"`
  414. }
  415. type Author struct {
  416. Name string `ini:"NAME"`
  417. Male bool
  418. Age int
  419. GPA float64
  420. NeverMind string `ini:"-"`
  421. *Embeded
  422. }
  423. func main() {
  424. a := &Author{"Unknwon", true, 21, 2.8, "",
  425. &Embeded{
  426. []time.Time{time.Now(), time.Now()},
  427. []string{"HangZhou", "Boston"},
  428. []int{},
  429. }}
  430. cfg := ini.Empty()
  431. err = ini.ReflectFrom(cfg, a)
  432. // ...
  433. }
  434. ```
  435. 瞧瞧,奇迹发生了。
  436. ```ini
  437. NAME = Unknwon
  438. Male = true
  439. Age = 21
  440. GPA = 2.8
  441. [Embeded]
  442. Dates = 2015-08-07T22:14:22+08:00|2015-08-07T22:14:22+08:00
  443. places = HangZhou,Boston
  444. ```
  445. #### 名称映射器(Name Mapper)
  446. 为了节省您的时间并简化代码,本库支持类型为 [`NameMapper`](https://gowalker.org/gopkg.in/ini.v1#NameMapper) 的名称映射器,该映射器负责结构字段名与分区名和键名之间的映射。
  447. 目前有 2 款内置的映射器:
  448. - `AllCapsUnderscore`:该映射器将字段名转换至格式 `ALL_CAPS_UNDERSCORE` 后再去匹配分区名和键名。
  449. - `TitleUnderscore`:该映射器将字段名转换至格式 `title_underscore` 后再去匹配分区名和键名。
  450. 使用方法:
  451. ```go
  452. type Info struct{
  453. PackageName string
  454. }
  455. func main() {
  456. err = ini.MapToWithMapper(&Info{}, ini.TitleUnderscore, []byte("package_name=ini"))
  457. // ...
  458. cfg, err := ini.Load([]byte("PACKAGE_NAME=ini"))
  459. // ...
  460. info := new(Info)
  461. cfg.NameMapper = ini.AllCapsUnderscore
  462. err = cfg.MapTo(info)
  463. // ...
  464. }
  465. ```
  466. 使用函数 `ini.ReflectFromWithMapper` 时也可应用相同的规则。
  467. #### 值映射器(Value Mapper)
  468. 值映射器允许使用一个自定义函数自动展开值的具体内容,例如:运行时获取环境变量:
  469. ```go
  470. type Env struct {
  471. Foo string `ini:"foo"`
  472. }
  473. func main() {
  474. cfg, err := ini.Load([]byte("[env]\nfoo = ${MY_VAR}\n")
  475. cfg.ValueMapper = os.ExpandEnv
  476. // ...
  477. env := &Env{}
  478. err = cfg.Section("env").MapTo(env)
  479. }
  480. ```
  481. 本例中,`env.Foo` 将会是运行时所获取到环境变量 `MY_VAR` 的值。
  482. #### 映射/反射的其它说明
  483. 任何嵌入的结构都会被默认认作一个不同的分区,并且不会自动产生所谓的父子分区关联:
  484. ```go
  485. type Child struct {
  486. Age string
  487. }
  488. type Parent struct {
  489. Name string
  490. Child
  491. }
  492. type Config struct {
  493. City string
  494. Parent
  495. }
  496. ```
  497. 示例配置文件:
  498. ```ini
  499. City = Boston
  500. [Parent]
  501. Name = Unknwon
  502. [Child]
  503. Age = 21
  504. ```
  505. 很好,但是,我就是要嵌入结构也在同一个分区。好吧,你爹是李刚!
  506. ```go
  507. type Child struct {
  508. Age string
  509. }
  510. type Parent struct {
  511. Name string
  512. Child `ini:"Parent"`
  513. }
  514. type Config struct {
  515. City string
  516. Parent
  517. }
  518. ```
  519. 示例配置文件:
  520. ```ini
  521. City = Boston
  522. [Parent]
  523. Name = Unknwon
  524. Age = 21
  525. ```
  526. ## 获取帮助
  527. - [API 文档](https://gowalker.org/gopkg.in/ini.v1)
  528. - [创建工单](https://github.com/go-ini/ini/issues/new)
  529. ## 常见问题
  530. ### 字段 `BlockMode` 是什么?
  531. 默认情况下,本库会在您进行读写操作时采用锁机制来确保数据时间。但在某些情况下,您非常确定只进行读操作。此时,您可以通过设置 `cfg.BlockMode = false` 来将读操作提升大约 **50-70%** 的性能。
  532. ### 为什么要写另一个 INI 解析库?
  533. 许多人都在使用我的 [goconfig](https://github.com/Unknwon/goconfig) 来完成对 INI 文件的操作,但我希望使用更加 Go 风格的代码。并且当您设置 `cfg.BlockMode = false` 时,会有大约 **10-30%** 的性能提升。
  534. 为了做出这些改变,我必须对 API 进行破坏,所以新开一个仓库是最安全的做法。除此之外,本库直接使用 `gopkg.in` 来进行版本化发布。(其实真相是导入路径更短了)