Create a web server in Go language (net / http) (1)

Obviously, you can set up a web server in golang (Go language), which allows web programming.

I also have experience in creating simple web servers with golang and developing services. At that time, I used a web framework called " gin </ font>".

gin is here

This time, I would like to make a web server with only the library (pure golang) without relying on the web framework. I referred to the official documentation https://golang.org/doc/articles/wiki/. If you want to study in the original English, please come

The basic grammar of golang is not covered here. Please study in advance. My recommendation is also the official documentation "A Tour of Go" </ font>. Click here for the link → "A Tour of Go"

Also, since programming is done in the Windows environment this time, if the commands etc. are different, please try with the command that suits your OS each time.

1. Create a program to set up a server First, create a directory called "golang_server" in GOPATH. I have a directory structure of "GOPATH / src / Go_Scripts / golang_server". Please create a directory in any location and move to the directory.
GOPATH\src\Go_Scripts>mkdir golang_server
GOPATH\src\Go_Scripts>cd golang_server
GOPATH\src\Go_Scripts\golang_server>

We will build a program in this directory.

server1.go


package main

import (
	"fmt"
	"log"
	"net/http"
)

func handler(w http.ResponseWriter, r *http.Request) {
	text := "You-saku."
	fmt.Fprint(w, "Hi there, I'm ", text)
}

func main() {
	http.HandleFunc("/", handler)
	log.Fatal(http.ListenAndServe(":8080", nil))
}

Let's explain the code. First of all, the packages required to set up a web server

1."log" 2."net/http" To import. It won't start without this.

Next, consider the functions "handler" and "main".

func handler(w http.ResponseWriter, r *http.Request) {
	text := "You-saku."
	fmt.Fprint(w, "Hi there, I'm ", text)
}

func main() {
	http.HandleFunc("/", handler)
	log.Fatal(http.ListenAndServe(":8080", nil))
}

There is a description "http.HandleFunc ("/ ", handler)" </ strong> in the main function. This is a method called HandleFunc of the http module, and the argument is ("access URL", request processing method) </ font>.

By the way, if the first argument is "/", it will be localhost. Also, for example, if you specify "/ profile" as the first argument, the request will be processed when you access with "localhost: port / profile". You can get a better understanding by changing the code yourself.

Next is the "handler" </ strong> function called in http.HandleFunc. This is the function that handles the actual request. As a rule, the argument is (variable http.ResponseWriter, variable * http.Request) </ font>. It's like a rule, so keep it in mind.

By the way, http.ResponceWriter secures a place to write on the web server. So fmt.Fprintf in the handler function receives "w" </ strong> as an argument. I write to this w and display it on the web page.

Finally, "log.Fatal" </ strong> is explained. This is to record the communication log. It takes "http.ListenAndServe (": 8080 ", nil)" </ strong> as an argument, but if an error occurs in http.ListenAndServe, it will be recorded. Also, "http.ListenAndServe ()" </ strong> is a function that sets which port number is actually used for http communication. The argument will be (": port number", error) </ font>. This time, I specified the port number 8080. Please note that when specifying the port number, please use : port number </ font>. If ":" is omitted, an error will occur.

2. Program execution and confirmation Now let's run the program.
GOPATH\src\Go_Scripts\golang_server>go run server1.go

Then some people will get a security warning. In that case, click "Allow access".

Then access [http: // localhost: 8080 /](http: // localhost: 8080 /) with your browser. Then you can access such a page. go_http1.png

I think it's a good idea to change the characters displayed by yourself.

Finally How was it? From the perspective of myself who has used gin, I had the impression that there were many parts to write code. I have to make various things myself, but I feel that I was able to learn more about golang. Next, we will use the "html / template" package that works with .html files to make it more like a web page.

Thank you for reading until the end.

Recommended Posts