Network Addresses and Named Ports
Network address and port information defines server service to listen on which host and port.
go
’s http
package provides ListenAndServe
function. According to
godoc:
func ListenAndServe(addr string, handler Handler) error
ListenAndServe
function accepts two arguments. The first one is addr
which is host:port definition. We can omit host information and
define network address as ":3000"
.
It’s possible to use named ports. On Linux/Mac all the network services
are defined under /etc/services
file:
#
# Network services, Internet style
#
# WELL KNOWN PORT NUMBERS
#
rtmp 1/ddp #Routing Table Maintenance Protocol
tcpmux 1/udp # TCP Port Service Multiplexer
tcpmux 1/tcp # TCP Port Service Multiplexer
# Mark Lottor <MKL@nisc.sri.com>
nbp 2/ddp #Name Binding Protocol
compressnet 2/udp # Management Utility
compressnet 2/tcp # Management Utility
compressnet 3/udp # Compression Process
compressnet 3/tcp # Compression Process
# Bernie Volz <VOLZ@PROCESS.COM>
echo 4/ddp #AppleTalk Echo Protocol
# 4/tcp Unassigned
# 4/udp Unassigned
rje 5/udp # Remote Job Entry
rje 5/tcp # Remote Job Entry
# Jon Postel <postel@isi.edu>
zip 6/ddp #Zone Information Protocol
# 6/tcp Unassigned
# 6/udp Unassigned
echo 7/udp # Echo
echo 7/tcp # Echo
#
#
http 80/udp www www-http # World Wide Web HTTP
http 80/tcp www www-http # World Wide Web HTTP
#
This list goes on and on… So, you can set :http
as addr
argument in ListenAndServe
function. Also you can use this convention almost everywhere! Enjoy named port
definitions.