go语言学习之gin框架开发web

我爱海鲸 2024-06-19 20:09:02 go语言学习

简介go、gin、重写个人博客、haijin-web-gin、打包静态资源

Gin Web Framework

1、idea gin开发web

我们直接参考官方文档

https://gin-gonic.com/zh-cn/docs/

首先安装go开发环境

初学go语言

在idea中我们需要安装go的插件才能进行go的开发

安装完毕后,我们首先新建一个项目,如图:

设置Environment:值为 GOPROXY=https://goproxy.cn,direct  (固定配置)

2、参考文档,快速入门

https://gin-gonic.com/zh-cn/docs/quickstart/

安装gin

 go get -u github.com/gin-gonic/gin

如图:

添加配置:

创建一个包start

Start.go:

package main

import "github.com/gin-gonic/gin"

func main() {
	r := gin.Default()
	r.GET("/ping", func(c *gin.Context) {
		c.JSON(200, gin.H{
			"message": "pong",
		})
	})
	r.Run() // 监听并在 0.0.0.0:8080 上启动服务
}

注意package main 必须为main,不然是没法运行的

完整截图如下:

点击运行:

在浏览器中,访问

http://localhost:8080/ping

返回:

{"message":"pong"}

即为运行成功
2023-07-06 start:
添加一个登录的接口案例:

安装包:go get -u github.com/go-sql-driver/mysql

package main

import (
	"database/sql"
	"fmt"
	"github.com/gin-gonic/gin"
	_ "github.com/go-sql-driver/mysql"
	"net/http"
)

type User struct {
	ID       int    `json:"id"`
	Username string `json:"username"`
	Password string `json:"password"`
}

type LoginForm struct {
	Username string `json:"username" binding:"required"`
	Password string `json:"password" binding:"required"`
}

func main() {
	router := gin.Default()

	router.POST("/login", handleLogin)

	router.Run(":8080")
}
func handleLogin(c *gin.Context) {
	var form LoginForm

	if err := c.ShouldBindJSON(&form); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}

	db, err := sql.Open("mysql", "root:123456@tcp(localhost:3306)/test")
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": "无法连接到数据库"})
		return
	}
	defer db.Close()

	var user User
	err = db.QueryRow("SELECT id, name, password FROM user WHERE name = ?", form.Username).Scan(&user.ID, &user.Username, &user.Password)
	if err != nil {
		c.JSON(http.StatusUnauthorized, gin.H{"error": "用户名或密码错误"})
		return
	}
	fmt.Printf("%d,%s,%s\n", user.ID, user.Username, user.Password)
	if form.Password != user.Password {
		c.JSON(http.StatusUnauthorized, gin.H{"error": "用户名或密码错误"})
		return
	}

	// 登录成功,返回成功消息
	c.JSON(http.StatusOK, gin.H{"message": "登录成功"})
}

end

2023-08-09 start

$GOPATH/go.mod exists but should not

end

2024-06-19 start:

打包静态资源:

首先安装:go get -u github.com/gobuffalo/packr/v2/packr

使用:

box := packr.NewBox("./static")
	//box := packr.New("staticBox", "./static")

	router.GET("/", func(c *gin.Context) {
		indexFileBytes, err := box.Find("index.html")
		if err != nil {
			c.AbortWithError(http.StatusNotFound, err)
			return
		}
		c.Data(http.StatusOK, "text/html; charset=utf-8", indexFileBytes)
	})

	//router.StaticFS("/static", gin.Dir("./static", true))
	//
	//router.GET("/", func(c *gin.Context) {
	//	c.Redirect(302, "/static") 
	//})

静态资源打包:

Go 1.16 and above
$ go install github.com/gobuffalo/packr/v2@v2.8.3
or

$ go install github.com/gobuffalo/packr/v2@latest
Go 1.15 and below
$ go get -u github.com/gobuffalo/packr/...
Binary Installation
Go 1.16 and above
$ go install github.com/gobuffalo/packr/v2/packr2@v2.8.3
or

$ go install github.com/gobuffalo/packr/v2/packr2@latest
Go 1.15 and below
$ go get -u github.com/gobuffalo/packr/packr2

 packr2

然后再打包:

 go build

end






你好:我的2025