How to convert a string to a constant?

I need to use http.Get() function which accepts a constant url.
On passing a string(accepting from user) to it throws a warning “G107: Potential HTTP request made with variable url”

Can you please share a minimal example, I have never seen that warning…

To your question though, you can create a constant string using const, though that is contradicted by “accepting from user”. There is no way to convert a runtime provided value to a constant, for which the literal value has to be known at compiletime.

func fetch (url string) {

var resp *http.Response
var err error

resp, err = http.Get(url)  //throws warning

if err != nil {
	return nil, err
}

resp.Body.Close()

}

I use similar code in my everyday stuff, I don’t see any warnings there. Neither at compile nor runtime. Can you please elaborate where the warning is “thrown”?

It sounds like you’re using the gosec tool provided by securego. The warning is because you’re accepting a URL from the user. The resolution is to not accept URL input from the user because the user might provide you with a malicious URL that your program will Get and possibly then do something unsafe. There might be a way to disable that warning but I don’t know how. You’ll have to check that linter’s documentation.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.