feat(*): go 后端项目脚手架
This commit is contained in:
18
pkg/common/rand.go
Normal file
18
pkg/common/rand.go
Normal file
@@ -0,0 +1,18 @@
|
||||
package common
|
||||
|
||||
import (
|
||||
"math/rand"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Rand 生成指定长度的随机字符串,字符集为 [0-9a-zA-Z]
|
||||
func Rand(length int) string {
|
||||
const charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
|
||||
|
||||
seededRand := rand.New(rand.NewSource(time.Now().UnixNano()))
|
||||
b := make([]byte, length)
|
||||
for i := range b {
|
||||
b[i] = charset[seededRand.Intn(len(charset))]
|
||||
}
|
||||
return string(b)
|
||||
}
|
||||
35
pkg/common/response.go
Normal file
35
pkg/common/response.go
Normal file
@@ -0,0 +1,35 @@
|
||||
package common
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type Response[T any] struct {
|
||||
Code int `json:"code" example:"0"`
|
||||
Msg string `json:"msg" example:"success"`
|
||||
Data T `json:"data,omitempty"`
|
||||
}
|
||||
|
||||
func Succ(c *gin.Context, data interface{}) {
|
||||
c.JSON(http.StatusOK, Response[interface{}]{
|
||||
Code: 0,
|
||||
Msg: "success",
|
||||
Data: data,
|
||||
})
|
||||
}
|
||||
|
||||
func Ok(c *gin.Context, msg string) {
|
||||
c.JSON(http.StatusOK, Response[interface{}]{
|
||||
Code: 0,
|
||||
Msg: msg,
|
||||
})
|
||||
}
|
||||
|
||||
func Fail(c *gin.Context, msg string) {
|
||||
c.JSON(http.StatusBadRequest, Response[interface{}]{
|
||||
Code: -1,
|
||||
Msg: msg,
|
||||
})
|
||||
}
|
||||
10
pkg/common/sha1.go
Normal file
10
pkg/common/sha1.go
Normal file
@@ -0,0 +1,10 @@
|
||||
package common
|
||||
|
||||
import (
|
||||
"crypto/sha1"
|
||||
"encoding/hex"
|
||||
)
|
||||
|
||||
func Sha1(pwd string) string {
|
||||
return hex.EncodeToString(sha1.New().Sum([]byte(pwd)))
|
||||
}
|
||||
Reference in New Issue
Block a user