golang with compile error: undefined: strings in strings.trim

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

golang with compile error: undefined: strings in strings.trim

问题

我尝试使用golang解决这个问题557. 反转字符串中的单词 III。我的代码如下:

import "fmt"
import ss "strings"

func reverseWords(s string) string {
    words := ss.Split(s, " ")
    res := ""
    for i := 0; i < len(words); i++ {
        curWord := ss.Split(words[i], "")
        for j := len(curWord) - 1; j >= 0; j-- {
            res += curWord[j]
        }
        if i != len(words)-1 {
            res += " "
        }
    }
    return res
}

func main() {
    s := "Let's take LeetCode contest"
    fmt.Println(reverseWords(s))
}

在我的电脑上一切正常,至少可以通过编译。然而,当我在LeetCode上提交时,它告诉我:

Line 67: undefined: strings in strings.Trim

我在谷歌上搜索了这个错误,但没有找到相关的信息。作为一个golang的初学者,我需要帮助。非常感谢你的帮助。

英文:

I try to use golang deal with this problem 557. Reverse Words in a String III
my code as below:

import &quot;fmt&quot;
import  ss &quot;strings&quot;

func reverseWords(s string) string {
    words := ss.Split(s,&quot; &quot;);
    res := &quot;&quot;;
    for i:=0; i &lt; len(words);i++{
	    curWord := ss.Split(words[i],&quot;&quot;);
	    for j:=len(curWord)-1; j &gt;= 0;j--{
		    res += curWord[j];
	    }
	    if(i!=len(words)-1){
		    res += &quot; &quot;;
	    }
    }
    return res;
}

func main(){
    s := &quot;Let&#39;s take LeetCode contest&quot;;
    fmt.Println(reverseWords(s));
}

Everything is ok in my pc, it can pass compile at least. <br>
However, when I submit in leetcode it tell me :

 Line 67: undefined: strings in strings.Trim 

I google this error but get nothing relevant info. As a beginner in golang, I need help. Everything will be appreciated.

答案1

得分: 6

你正在使用别名导入字符串:

import ss "strings"

这意味着在该文件的任何地方,你必须使用ss来引用strings,例如:

words := ss.Split(s, " ")

如果你使用默认的导入方式:

import "strings"

那么你可以像平常一样使用strings来引用它。

请注意,目前被接受的答案有两个错误:你绝对可以像你所做的那样使用别名,只是你必须使用别名来引用该包。如果你使用你给它起的名字,不会引起任何问题。其次,如果你想引用它,你绝对需要导入strings包,无论是否使用别名,这是你的选择。

另外,你应该强烈考虑在你的代码上运行go fmt,因为它不符合Go的编码标准;例如,标准的Go代码省略了绝大部分的分号。代码仍然可以工作,但如果你的代码格式符合其他Go开发者习惯的方式,你将更容易得到其他Go开发者的帮助。

英文:

You're importing strings under an alias:

import  ss &quot;strings&quot;

That means that everywhere in that file, instead of referring to strings you must refer to ss, for example:

words := ss.Split(s,&quot; &quot;)

If you use the default import:

import &quot;strings&quot;

Then you can refer to it as strings as normal.

Note that the currently accepted answer is wrong about two things: you can absolutely use the alias as you have it, you just have to refer to the package with the aliased name. It will not cause any issues if you use the name you gave it. Second, you absolutely do need to import the strings package - with or without an alias, your choice - if you want to refer to it.

On a completely unrelated side note, you should strongly consider running go fmt on your code, as it does not follow Go coding standards; for example, standard Go code omits the vast majority of semicolons. The code will work regardless, but you'll have an easier time getting help from other Go developers if your code is formatted the way everyone else is used to seeing it.

答案2

得分: 0

如果您使用不同的名称导入strings包,那么它将会引发问题,因为包名被包装代码用于完整运行函数。

不需要再次导入strings包。它已经被添加了。

直接使用它即可。

func reverseWords(s string) string {
    words := strings.Split(s, " ");
    res := "";
    for i := 0; i < len(words); i++ {
        curWord := strings.Split(words[i], "");
        for j := len(curWord) - 1; j >= 0; j-- {
            res += curWord[j];
        }
        if i != len(words)-1 {
            res += " ";
        }
    }
    return res;
}
英文:

If you import strings package with different name then it will cause issue as it is used by the wrapper code to run the function completely.

No need to import strings package again. It will be added.

Just use it directly.

func reverseWords(s string) string {
    words := strings.Split(s,&quot; &quot;);
    res := &quot;&quot;;
    for i:=0; i &lt; len(words);i++{
        curWord := strings.Split(words[i],&quot;&quot;);
        for j:=len(curWord)-1; j &gt;= 0;j--{
            res += curWord[j];
        }
        if(i!=len(words)-1){
            res += &quot; &quot;;
        }
    }
    return res;
}

huangapple
  • 本文由 发表于 2017年5月17日 14:43:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/44017428.html
匿名

发表评论

匿名网友

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

确定