go

Extract part of string in Golang?


I'm learning Golang so I can rewrite some of my shell scripts.

I have URL's that look like this:

https://example-1.example.com/a/c482dfad3573acff324c/list.txt?parm1=value,parm2=value,parm3=https://example.com/a?parm1=value,parm2=value

I want to extract the following part:

https://example-1.example.com/a/c482dfad3573acff324c/list.txt

In a shell script I would do something like this:

echo "$myString" | grep -o 'http://.*.txt'

What is the best way to do the same thing in Golang, only by using the standard library?


Solution

  • There are a few options:

    // match regexp as in question
    pat := regexp.MustCompile(`https?://.*\.txt`)
    s := pat.FindString(myString)
    
    // everything before the ?
    s, _, _ := strings.Cut(myString, "?")
    
    // parse and clear query string
    u, err := url.Parse(myString)
    u.RawQuery = ""
    s := u.String()
    

    The last option is the best because it will handle all possible corner cases.

    Try it on the playground