Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Go: Missing port in address

Tags:

http

tcp

go

I was trying to make a simple webapp in Go/Golang. My main fuction code is :

func main() {
fileServer := http.FileServer(http.Dir("./static"))
http.Handle("/", fileServer)
http.HandleFunc("/form", formHandler)
http.HandleFunc("/hello", helloHandler)

fmt.Printf("Starting server at port 8080\n")
if err := http.ListenAndServe(":8080", nil); err != nil {
    log.Fatal(err)
}

}

When I run the go run main.go I get the following error:

listen tcp: address ;8080: missing port in address
exit status 1

I tried using the lsof -i :8080 command but got no output. Chaging the port to :80 from :8080 had no effect. nc -l :8080 didn't work too.

How do I resolve this issue?

PS: I'm using Fedora OS

like image 716
DevOpsnoob Avatar asked Oct 16 '25 23:10

DevOpsnoob


1 Answers

I know this is old but incase of anyone else who may have this issue. Try specifying the port as "localhost:8080" instead of just ":8080"

In this case, the code should be like this:

func main() {
     fileServer := http.FileServer(http.Dir("./static"))
     http.Handle("/", fileServer)
     http.HandleFunc("/form", formHandler)
     http.HandleFunc("/hello", helloHandler)

     fmt.Printf("Starting server at port 8080\n")
     if err := http.ListenAndServe("localhost:8080", nil); err != nil {
        log.Fatal(err)
     }
like image 130
Cocoas Avatar answered Oct 18 '25 16:10

Cocoas



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!