main.go:9: 使用了未选择的 str 包

huangapple go评论80阅读模式
英文:

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:

Docs: https://golang.org/pkg/builtin/#string

huangapple
  • 本文由 发表于 2017年6月19日 22:45:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/44633596.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定