Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

go

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

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));
}

Everything is ok in my pc, it can pass compile at least.
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.

like image 591
nail fei Avatar asked May 17 '17 06:05

nail fei


People also ask

How do I trim a string in Golang?

res1 := strings. Trim(str1, "!" ) 2. TrimLeft: This function is used to trim the left-hand side(specified in the function) Unicode code points of the string.

How do you check if a string is empty or not in Golang?

In Golang, we can do this by: Comparing it with an empty string. Evaluating the length of the string; if it's zero, then the string is empty, and vice versa.

Is empty string false in Golang?

The zero value is: 0 for numeric types, false for the boolean type, and. "" (the empty string) for strings.

Where can I find substring in Golang?

Go – Check if String contains a Substring To check if string contains a substring in Go, call Contains function of strings package and provide the string and substring as arguments to the function. Contains function returns a boolean value. It returns true if the substring is present in the string, or false if not.


1 Answers

You're importing strings under an alias:

import  ss "strings"

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

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

If you use the default import:

import "strings"

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.

like image 73
Adrian Avatar answered Sep 28 '22 00:09

Adrian