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.
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.
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.
I think it's a good idea to change the characters displayed by yourself.
Thank you for reading until the end.
Recommended Posts