linuxwindowsgoubuntuwindows-subsystem-for-linux

go run unable to serve website in user mode, however works in root mode in ubuntu WSL - Windows


I am writing a simple web app in golang: golang http main.go server, with vscode user mode invocation

the server snippet:

package main

import (
    "fmt"
    "net/http"
)

func helloWorldPage(w http.ResponseWriter, r *http.Request){
    fmt.Fprint(w, "Hello world!")
}

func main(){
    http.HandleFunc("/", helloWorldPage)
    http.ListenAndServe("", nil)

}

The problem is invocations from terminal via vscode (or simple user mode for that matter) immediately terminates without serving at default port 80.

drainbamage@LAPTOP-KPETF9CD:~/dev/portfolio$ go version
go version go1.22.3 linux/amd64
drainbamage@LAPTOP-KPETF9CD:~/dev/portfolio$ go run main.go   <--- terminates without hosting.
drainbamage@LAPTOP-KPETF9CD:~/dev/portfolio$

However... if I run the same command in root mode, it works as expected:

drainbamage@LAPTOP-KPETF9CD:~/dev/portfolio$ sudo su
[sudo] password for drainbamage:
root@LAPTOP-KPETF9CD:/home/drainbamage/dev/portfolio# go run main.go   <--- working as expected.
^Csignal: interrupt
root@LAPTOP-KPETF9CD:/home/drainbamage/dev/portfolio#

root mode main.go invocation

vscode prompt

hosted webpage

Tried

Expected


Solution

  • As @MikeChristensen suggested, one cannot dabble with ports lower than 1024 without root permissions. (More info that I found - post)

    Solution?

    log.Fatal(http.ListenAndServe(":8080", nil))