A "hello world" web server

Create a directory called key-value-store, and in that directory run go mod init key-value-store. Then make a new file called server.go and add the following to it:

Above we have got a lot of code, but this is the most basic Go web server.

In Go, packages are scoped by folder, so files in the same folder should all have the same package name. In this case, we have gone with the package name main. main is the name of any package that outputs an executable, and has a main() function defined in it.

Below the package definition, we have a list of imports. We have separated the packages in the standard library and any external dependencies. Usually if it looks like a URL, it is an external dependency. godoc.org has documentation for the latest versions of both the standard library packages. To get a package's documentation, just go to godoc.org/packagename. For example, godoc.org/github.com/go-chi/chi has the documentation for the Chi package which we will be discussing soon.

 

This page is a preview of Reliable Webservers with Go

Please select a discussion on the left.