英文:
main.go:9: use of package str without selector
问题
我在Tour of Go的解释器中有以下内容:
package main
import (
"golang.org/x/tour/wc"
str "strings"
)
func WordCount(s string) map[string]int {
results := make(map[string]int)
words := str.Fields(s)
return map[string]int{"x": 1}
}
//func main() {
// wc.Test(WordCount)
//}
这是基于https://tour.golang.org/moretypes/23的。
我的错误是:
tmp/sandbox169629521/main.go:9: use of package str without selector
尝试将
results := make(map[str.string]int)
更改为
results := make(map[string]int)
现在失败,显示以下错误:
tmp/sandbox424441423/main.go:9: cannot refer to unexported name strings.string
tmp/sandbox424441423/main.go:9: undefined: strings.string
英文:
I have the following in the intepreter of Tour of Go:
package main
import (
"golang.org/x/tour/wc"
str "strings"
)
func WordCount(s string) map[string]int {
results := make(map[str]int)
words := str.Fields(s)
return map[string]int{"x": 1}
}
//func main() {
// wc.Test(WordCount)
//}
This is based on https://tour.golang.org/moretypes/23
My error is
tmp/sandbox169629521/main.go:9: use of package str without selector
Trying
results := make(map[str.string]int)
now fails with
tmp/sandbox424441423/main.go:9: cannot refer to unexported name strings.string
tmp/sandbox424441423/main.go:9: undefined: strings.string
答案1
得分: 3
"string"是一个内置类型。你不需要使用strings.string:
文档:https://golang.org/pkg/builtin/#string
英文:
"string" is a builtin. You don't need to do strings.string:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论