初级会员
  • 第 13828 位会员
  • meijies
  • 2017-10-30 12:23:09
  • Offline
  • 19 90

最近发布的文章

    暂无

最近分享的资源

    暂无

最近发布的项目

    暂无

最近的评论

  • @polaris 谢谢,问题解决了
  • #4 @meijies ```console github.com/meijies/theme/collection/config ./document.go:21:29: cannot assign to struct field target.sub[subkey].value in map ./document.go:23:11: cannot use target.sub[subkey] (type doc) as type *doc in assignment ./document.go:33:10: cannot use target.sub[subkey] (type doc) as type *doc in assignment FAIL command-line-arguments [build failed] ```
  • #3 @polaris ![Screenshot from 2017-10-30 21-24-05.png](https://static.studygolang.com/171030/4422e36766e679ffb6b378f042c1d71a.png) 不好意思,一开始点错了
  • ![Screenshot from 2017-10-30 20-19-50.png](https://static.studygolang.com/171030/b599ad4bb42aab899d3ffb1984da7381.png)
  • ```go package config import "strings" type doc struct { value string sub map[string]doc } var root = new(doc) func Add(key string, value string) { keys := strings.Split(key, ".") var target = root for index, subkey := range keys { if target.sub == nil { target.sub = make(map[string]doc) target.sub[subkey] = doc{} } if index == len(keys)-1 { target.sub[subkey].value = value } else { target = target.sub[subkey] } } } func Get(key string) string { keys := strings.Split(key, ".") target := root for index, subkey := range keys { target = target.sub[subkey] if index == len(keys)-1 { return target.value } } return "" } ```