分类标签归档:Go

[ gin验证请求参数 ]


golang验证结构体

相当于验证用户上传的json参数

定义结构

package dto

type TestJsonReq struct {
    Name     string `json:"name" binding:"min=3,max=20,required"`
    Password string `json:"password" binding:"required"`
    Age      int    `json:"age" binding:"required

< Read more >

[ gin使用jwt ]


主要使用jwt/v5框架

package collections

import (
    "fmt"
    "time"

    "github.com/golang-jwt/jwt/v5"
)

var hmacSampleSecret = []byte("my_eyjwt_secret")

type TokenMap struct {
    Type     string
    Nbf      int64
    Username string
    UserId   int
    Exp 

< Read more >

[ golang创建环境 ]


golang创建环境-点击跳转官方文档

在当前文件夹下创建依赖跟踪mod

go mod init example/hello

chf@chf:~/go_obj/test$ cat go.mod

module test_mod

go 1.23.2

自动根据当前文件夹下使用到的模块,下载并且添加到mod里面

go mod tidy

配置mod使用关系

GO111MODULE开启或关闭模块支持 GO111MODULE=off 关闭模块支持,go 会从 GOPATH 和 vendor 文件夹寻找包。 GO111MODULE=on 开启模块支持,go 会忽略 GOPATH 和 vendo

< Read more >

[ golang安装 ]


golang下载安装-点击跳转官方文档

下载地址

Linux中安装

安装位置自行选择当前是/usr/local/

tar -C /usr/local -xzf go1.23.2.linux-amd64.tar.gz

您可以通过将以下行添加到 $HOME/.profile 或 /etc/profile(用于系统范围的安装):

export PATH=$PATH:/usr/local/go/bin

通过打开命令提示符并键入 Go 来验证是否已安装 Go 以下命令:

 go version

< Read more >

[ go使用pdf ]


gopdf gopdf is a simple library for generating PDF document written in Go lang.

A minimum version of Go 1.13 is required.

Features Unicode subfont embedding. (Chinese, Japanese, Korean, etc.) Draw line, oval, rect, curve Draw image ( jpg, png ) Set image mask Password protection Font kerning Install

< Read more >

[ find查询为空异常[xorm] ]


XORM异常

find查询为空

当使用切片find查询时, 传入切片或者map的指针*[]model.user就可以, 不要重复使用指针, 会导空

正确示范

domain.go业务

func (u UserDomain) UserList(page int64) ([]model.UserModel, error) {
    limit := page * 5
    offset := 5 * (page - 1)
    userList := make([]model.UserModel, 0)
    err := u.userRepo.UserList(limit, offse

< Read more >