I have a Gorilla Mux router set up in Go. I have routes set up within that router, as well as function handlers associated with those routes. The router works perfectly, if you open a browser window and enter specific URLs. However, the problem I'm running into is what to do if the URL is entered on the command line. I know how to store the URL from the command line arguments, but I don't know how to forward the URL, stored as a URL variable in Go, to the router. Like, how do you call a route's function handler if the URL is given on the command line INSTEAD of entered via browser window?
Code:
u, err := url.Parse(os.Args[1])
if err != nil {
fmt.Println(err.Error())
}
host, port, _ := net.SplitHostPort(u.Host)
s := []string{":", port};
router := ANewRouter()
log.Fatal(http.ListenAndServe(strings.Join(s, ""), router))
//Route URL to router, somehow
If you're trying to make an HTTP request in Go there are easy to use methods for that purpose in the standard library at; https://golang.org/pkg/net/http/
As JimB pointed out in comment you could use something like curl
and just forgo using Go altogether. Launching curl
from Go doesn't make a whole lot of sense to me when you can just do resp, err := http.Get(urlArg)
and get the same result. There are also methods for other HTTP verbs and if you need more fine tuned control of the request you can use the Do
method and create Request
object to do things like set headers.