Every project needs a portal to make it work. In the previous section, we mentioned that the main directory is the entry directory and main() is the entry function of Golang. Therefore, we create a main.go file in the main directory as the entry file and write the main() function in it.

Taking a

package main

import "fmt"

func main(a) {
	fmt.Println("Hello World")}Copy the code

This simply prints Hello World to the console. So let’s just run it and see if we can get Hello World.

Running the test is very simple. After we are ready early, there is a solid green triangle in front of the main() function, click on it and select the first menu to run it

After running, you should see the output execution result in the console:Good, we’ve got Go running successfully, and it will be our entry point for the blog project from here to run the code.

Of course, a Single Hello World is not enough for our project, we have a lot of requirements waiting to be implemented. Here, let’s rewrite our main() function to make it work for us.

The use of the iris

Iris currently has two versions, and we choose to use irIS12 version.

bootstrap.go

First, we create a bootstrap.go file in the root directory and place iris’s logic code in this file.

The code for the entire file is as follows:

package irisweb

import (
	"context"
	"fmt"
	"github.com/kataras/iris/v12"
)

type Bootstrap struct {
	Application *iris.Application
	Port        int
	LoggerLevel string
}

func New(port int, loggerLevel string) *Bootstrap {
	var bootstrap Bootstrap
	bootstrap.Application = iris.New()
	bootstrap.Port = port
	bootstrap.LoggerLevel = loggerLevel

	return &bootstrap
}

func (bootstrap *Bootstrap) Serve(a) {
	bootstrap.Application.Logger().SetLevel(bootstrap.LoggerLevel)

	bootstrap.Application.Get("/".func(ctx iris.Context) {
		ctx.WriteString("Hello World")
	})

	bootstrap.Application.Run(
		iris.Addr(fmt.Sprintf("127.0.0.1: % d", bootstrap.Port)),
		iris.WithoutServerError(iris.ErrServerClosed),
		iris.WithoutBodyConsumptionOnUnmarshal,
	)
}
Copy the code

The entire file is relatively simple:

  • A Bootstrap structure that holds iris objects, TCP ports, and logging level information.
  • A New function that initializes IRIS, as well as side assignment, log level assignment, and so on.
  • Another function is the IRIS Startup service function, which starts setting up the initialization information for the IRIS run. This configuration information is derived from information stored in config.json.
  bootstrap.Application.Get("/".func(ctx iris.Context) {
		ctx.WriteString("Hello World")})Copy the code

This defines an access route, and the anonymous function is the function that is executed when accessing this route. Iris supports multiple routes, including Get, Post, Put, and Delete, and Party to group routes.

  bootstrap.Application.Run(
		iris.Addr(fmt.Sprintf("127.0.0.1: % d", bootstrap.Port)),
		iris.WithoutServerError(iris.ErrServerClosed),
		iris.WithoutBodyConsumptionOnUnmarshal,
	)
Copy the code

This part of the code uses Iris to identify the specified port, which is the port we defined in config.json. In addition to setting the port, there is support for adding other configuration items, such as WithoutServerError which ignores errors. WithoutBodyConsumptionOnUnmarshal run iris body can consumption for many times, that is to say, you are in the middleware is used in the body, in the subsequent processing function, can also use the body again. If you don’t set this configuration item, the body can only be used once and the content won’t be available again.

main.go

The logic for Iris is already written above, but we need to put it in main.go to get the code running. So let’s delete hello world and rewrite main.go:

package main

import (
	"irisweb"
	"irisweb/config"
)

func main(a) {
	b := irisweb.New(config.ServerConfig.Port, config.ServerConfig.LogLevel)
	b.Serve()
}
Copy the code

As shown above, the output of content is not in the console, but in the browser. Let’s try it out. Click on the green solid triangle to the left of the main.go function and run it to see what happens.

Once it’s running, type it in your browserhttp://127.0.0.1:8001Access. The port 8001 set here is the port set by config.json. If the port set by you is not 8001, please change it to the port set by you. If nothing else, you should see the following output:

Congratulations, your configuration has been successful. Just printing Hello World in the browser, of course, is not enough for our needs. In the next section, we will follow up our requirements document to improve our blog site. Let’s modify bootstrap.go.

The complete project sample code is hosted on GitHub. The complete project code can be viewed at github.com/fesiong/gob… You can also fork a copy to make changes on it.