I know that Golang contain native built-in web server(net/http) that can be used as server without using external web servers(apache,nginx,etc). For local development, you simply run http.ListenAndServe
and your server is ready locally.
My question is, How to set up your golang app to be accessed by others publicly without the need of external web servers?
In your golang code, you don't need to do anything else except specifying the port your app listening to. After setting the port(e.g :8080
), you need to do these things to make it accessible:
- If you want your app to be accessible internally(LAN/WLAN), check your private IP address, then access it from any other devices within your internal network. For example your private ip is 10.50.50.10 then access it with your port specified in your app:
10.50.50.10:8080
- If you want your app to be accessible publicly(WAN), check your public IP. First you need to make sure the port you're using for your app to listen. Set your firewall to let the port open for public access. Then You must forward the port in the router. Port forwarding is done to let any request to your public IP and specific port you set to be forwarded to your device's IP(private IP) and the port your app listening. Search for port forwarding for specific OS and router you used for further information.