Basic web server in Golang

Yoplux
2 min readMay 3, 2021

--

From Golang website

The Golang programming language seems to be the future, first because it comes from Google, in a second time because it is a robust system-level language used for programming across large-scale network servers and big distributed systems such as ………..GOOGLE !

Here is a very basic web server i want to share with you, it is possible to see it on the Golang project website.

package main// package “main” tells the Go compiler that the package should compile as an executable
import ("fmt""log""net/http")//http.ResponseWriter value assembles the HTTP server's response, http.Request represent the client HTTP requestfunc handler(w http.ResponseWriter, r *http.Request) {//r.URL.Path is the path component of the request URLfmt.Fprintf(w, "Hello i'm a huge fan of %s!", r.URL.Path[1:])}func main() {//HandleFunc tells the http package to handle all requests to the root "/" with handlerhttp.HandleFunc("/", handler)//It then calls http.ListenAndServe specifying to listen on port 8080log.Fatal(http.ListenAndServe(":8080", nil))}

To compile in Go you first have to download the Go compiler from the website and then to compile the code above you have to type

go run .\yourfile.go

In my case it’s

go run .\server.go

Then go to localhost:8080/pizza

You understood that the path go into our %s into the code !

I started learning golang since two days so i will be back with a better web server in a month :)

Thanks !

Signed Alix.

--

--