Use of go framework beego: Practical combat

beego actual combat

§ Use of go framework beego: Practical combat

Preface

The tutorial for configuring beego is in my other blog post:https://blog.csdn.net/Acegem/article/details/129387963?spm=1001.2014.3001.5501

1. Create a new project

bee new myproject

2. (sessionon) session fails to open and error is reported

Error message

[config.go:191] Handler crashed with error runtime error: invalid memory address or nil pointer dereference

Solution

This kind of error is mostly caused by the session not being opened or not being opened correctly. There are two ways to open the session:

Method 1: Configuration file (configure session to open)

Set in conf/app.conf:

sessionon = true

Pay special attention here!There cannot be comments after .conf files like app.conf!,as follows:
Error comment writing:

sessionon = true # session开关,true表启用session功能

Correct way to write comments:

# session开关,true表启用session功能
sessionon = true
Method 2: Main function (open session)

Write in the main function:beego.BConfig.WebConfig.Session.SessionOn = true

func main() {
    
    
	/**
	实现session启用可以通过conf/app.conf配置文件设置
	sessionon = true
	或者在main方法中写入
	beego.BConfig.WebConfig.Session.SessionOn = true
	*/
	beego.BConfig.WebConfig.Session.SessionOn = true
	beego.Run()
}

3. There are no processing steps for go.mod

cd 项目目录名

# 生成一个初始化的 go.mod 文件,文件内容与你之前安装的bee版本也有关。
go mod init 项目目录名

#** 需要联网 **#
# 将需要的包写入go.mod文件中,并生成 go.sum 文件
go get 项目目录名

# 执行,会自动下载go.mod需要的包。
bee run

4. An error is reported when executing bee run.Error: 0004 Failed to build the application: main.go:5:2: missing go.sum entry for module

Error message

main.go:5:2: missing go.sum entry for module providing package github.com/beego/beego/v2/server/web

ERROR    ▶ 0004 Failed to build the application: main.go:5:2: missing go.sum entry for module providing package github.com/beego/beego/v2/server/web

reason

It may be because the version of go is new. The latest version of go has enabled thego.mod mode, which is the package management tool, and the corresponding module package is not installed in the management package directory.

Solution

Method 1

enter

go build -mod=mod

to install the dependent packages ingo.mod (default is in the project directory).
Execute again

bee run

Note: Generally, go.mod files are generated in the project directory by default when using bee to initialize a new project (bee new project name). If there is no go.mod file, you need to use the go command to initialize one.

cd 项目目录名

# 生成一个初始化的 go.mod 文件,文件内容与你之前安装的bee版本也有关。
go mod init 项目目录名

#** 需要联网 **#
# 将需要的包写入go.mod文件中,并生成 go.sum 文件
go get 项目目录名

# 执行,会自动下载go.mod需要的包。
bee run
Method 2 (not verified)

Turn off go.mod

# go env 可列出所有go相关环境配置, -w 表写入
go env -w GO111MODULE=off

Attachment: Enter go env and you can see that GO111MODULE is enabled by default.
Insert image description here

Guess you like

Origin blog.csdn.net/Acegem/article/details/129389371