Running main.go to open a webpage from command line?


(Scott Laing) #1

(also what is ‘back tick’?? I used back ticks for the code and it looks like carp.)
I made a simple webpage, using go:


import (

"fmt"

"net/http"

)

func handlerFunc(w http.ResponseWriter, r *http.Request) {

fmt.Fprint(w, "<h1>Welcome to my website!</h1>")

}

func main() {

http.HandleFunc("/", handlerFunc)

http.ListenAndServe(":80", nil)

}```

When I try `>go run main.go` Windows opens a new command window temporarily, but not the browser page. I have confirmed that Go is installed correctly, the Go workspace is installed, all paths are installed.

How do I get this to open a browser?

(catacombs) #2

You just go to localhost:80. Your page should appear.

There’s no note about the page starting because you didn’t print a message!


(Norbert Melzer) #3

Check the return value of the call to http.ListenAndServe(), it will probably tell you that you can’t bind port 80, as you need to be superuser/administrator to do so.

Use a port that is greater than 1024.


(catacombs) #4

Ah, yes. 8080, 8000, or 3000 are good choices.


(Scott Laing) #5

Yep, have tried 8080, 8000, 3000 and others. It has just occurred to me — should I be keeping all Go work files in the Go workspace location (D:\Documents\Go)? Am completely new to Go so this is my first attempt at running a webpage from the command line. I am actually working in an xampp directory. I use xampp for other local development.

Have attached images of what exactly happens. The first are the popups that launch after running main.

Next, is my path info. I am pretty sure this is all correct.


(Zhong Qin) #6
  1. You can specify the port from range 1025 to 65535 in your PC.
  2. The popup window “Windows Security Alert” is to alert that there is a service(Your Go app) needs to use a port to communicate via the internet. Hit the Allow access button will do no harm to your PC since this app is just your Go web app.

Correct me if I was wrong.


(Norbert Melzer) #7

Please use fmt.Println to print the error result of listen and serve, and make sure that the window does not close before reading the output. As a last resort you can just throw a timer.Sleep after printing.

Then copy and paste the error you see.

Also, be need to have xampp, at least not the apache!


(Scott Laing) #8

The popup is just from the windows av.

I use xampp for other local development, without issues. It is running while I do this.

I did this to print the error and nothing printed:

func main() {
    http.HandleFunc("/", handlerFunc)
    http.ListenAndServe(":8000", nil)
    a := http.ListenAndServe(":8000", nil)
    fmt.Println(a)

}


(Norbert Melzer) #9

If this really doesn’t print anything, then your server is running and should serve your content. Have you tried opening http://localhost:8000?


(George Calianu) #10

Two small observations around:

  • Your above code serves pages so you need a browser to access the page served by this server. Don’t expect from your code to open any browser.
  • If you have other servers in your sistem take care about used ports and don’t use them in your Go program.

(Scott Laing) #11

BINGO!!!

So, this is my first foray into server programming… I have no idea what I am doing. I thought running go main opened a browser, but what it does instead is create something server-related, and I must then open a browser manually and enter the url. Doing that makes it all work as expected.

Thanks all for the patient and positive feedback!!!