Linux/macOS(适用于 bash、zsh)

# 建议安装前清空`GOROOT`、`GOBIN`等环境变量

curl -sSL https: //raw.githubusercontent.com/voidint/g/master/install.sh | bash

echo “unalias g” >> ~/.bashrc # 可选。若其他程序(如’git’)使用了’g’作为别名。

这里如果你是 oh-my-zsh 的用户,那么你还需要做一件事,就是解决全局的 g 命令的冲突,解决的方式有两种,第一种是在你的 .zshrc 文件末尾添加 unalias :

echo "unalias g" >> ~/.zshrc # 可选。若其他程序(如'git')使用了'g'作为别名。
# 记得重启 shell ,或者重新 source 配置

第二种,则是调整 ~/.oh-my-zsh/plugins/git/git.plugin.zsh 中关于 g 的注册,将其注释或删除掉:

# alias g='git'

我的 .zshrc 中的完整配置:

# 我的 g 的bin目录调整到了 .gvm ,所以你可能需要一些额外的调整
export PATH="${HOME}/.gvm/bin:$PATH"
export GOROOT="${HOME}/.g/go"
export PATH="${HOME}/.g/go/bin:$PATH"
export G_MIRROR=https://gomirrors.org/


使用

查询当前可供安装的stable状态的 go 版本
$ g ls-remote stable
  1.19.10
  1.20.5

安装目标 go 版本1.20.5
$ g install 1.20.5

查询已安装的 go 版本
$ g ls
  1.19.10
* 1.20.5

查询可供安装的所有 go 版本
$ g ls-remote


切换到另一个已安装的 go 版本
$ g use 1.19.10

卸载一个已安装的 go 版本
$ g uninstall 1.19.10


清空 go 安装包文件缓存
$ g clean 

查看 g 版本信息
g --version

更新 g 软件本身
$ g update

卸载 g 软件本身
$ g [self] uninstall


golang之UrlEncode编码/UrlDecode解码


package main

import(
"fmt"
"net/url"
)

func main() {
var urlStr string = "傻了吧:%:%@163& .html.html"
escapeUrl := url.QueryEscape(urlStr)
fmt.Println("编码:",escapeUrl)

enEscapeUrl, _ := url.QueryUnescape(escapeUrl)
fmt.Println("解码:",enEscapeUrl)
}

package controller

import (
“encoding/json”
“errors”
“fmt”
“extend/helper”
“github.com/bitly/go-simplejson”
“github.com/gin-gonic/gin”
“io/ioutil”
“log”
“net/http”
“sort”
“strings”
“time”
)

type TestController struct{}

//发布礼物配置
func (t *TestController) Index(c *gin.Context) {
result := make(map[string]interface{})
//result[“success”] = true
//result[“data”] = PostForm(c.Request)
//PostTest2(c)
//_, _ = c.GetPostForm(“sign1”)
//result[“postJson”] = PostTest(c.Request)
////result[“postJson2”] = PostTest3(c)
////result[“postJson1”] = Get(c.Request)
////result[“getC”] = getC
////result[“getD”] = d
//result[“Method”] = c.Request.Method
//result[“url”] = c.Request.URL
//result[“Proto”] = c.Request.Proto
//result[“Header”] = c.Request.Header
//result[“Host”] = c.Request.Host
////result[“GetBody”] = c.Request.GetBody
//result[“PostForm”] = c.Request.PostForm
//result[“MultipartForm”] = c.Request.MultipartForm
//result[“Trailer”] = c.Request.Trailer
//result[“RemoteAddr”] = c.Request.RemoteAddr
//result[“RequestURI”] = c.Request.RequestURI
//result[“Response”] = c.Request.Response

//data, err = verifySign(c)
//fmt.Println(data)
//fmt.Println(err)

result, _ = verifySign(c)

helper.Successful(c, result)
}

func verifySign(c *gin.Context) (data map[string]interface{}, err error) {
data = make(map[string]interface{})

req := c.Request
if req.Method == “GET” {
keys := req.URL.Query()
for k, v := range keys {
data[k] = v[0]
}
} else if req.Method == “POST” {
contentType := req.Header.Get(“Content-Type”)
if contentType == “application/x-www-form-urlencoded” {
req.ParseForm()
for k, v := range req.Form {
if len(v) < 1 { continue } data[k] = v[0] } } else if strings.Contains(contentType, "multipart/form-data;") { _ = c.PostForm("sign") for k, v := range req.PostForm { if len(v) < 1 { continue } data[k] = v[0] } } else if contentType == "application/json" { c.BindJSON(&data) } else { err = errors.New("错误的Content-type") } } else { err = errors.New("错误的Method") } return } //Get get请求url参数 func Get(req *http.Request) map[string]string { var result = make(map[string]string) keys := req.URL.Query() for k, v := range keys { result[k] = v[0] } return result } //PostForm 获取postform形式的参数 func PostForm(req *http.Request) map[string]string { //body, _ := ioutil.ReadAll(req.Body) var result = make(map[string]string) req.ParseForm() for k, v := range req.PostForm { if len(v) < 1 { continue } result[k] = v[0] } return result } //PostJson 获取post json参数 func PostJson(req *http.Request, obj interface{}) error { body, err := ioutil.ReadAll(req.Body) if err != nil { return err } err = json.Unmarshal(body, obj) if err != nil { return err } return nil } //Get get请求url参数 func PostTest(req *http.Request) map[string]interface{} { var result = make(map[string]interface{}) body, err := ioutil.ReadAll(req.Body) if err != nil { return nil } err = json.Unmarshal(body, result) if err != nil { return result } return nil } //Get get请求url参数 func PostTest2(c *gin.Context) { json := make(map[string]interface{}) //注意该结构接受的内容 c.BindJSON(&json) log.Printf("%v", &json) } //Get get请求url参数 func PostTest3(c *gin.Context) map[string]interface{} { jsons := make(map[string]interface{}) //注意该结构接受的内容 c.BindJSON(&jsons) cont := jsons["content"].(string) js, err := simplejson.NewJson([]byte(cont)) //反序列化 if err != nil { panic(err.Error()) } fmt.Println(js) return jsons } var AppSecret string = "111" // 验证签名 //func verifySign(c *gin.Context) (map[string]string, error) { // // //appKey = "demo" // //appSecret = "xxx" // //encryptStr = "param_1=xxx&param_2=xxx&ak="+appKey+"&ts=xxx" // //自定义验证规则 // //sn = MD5(appSecret + encryptStr + appSecret) // // // validate appid redirectURI and return stat code scope // //var urlStr string = "https://www.google.com/" // https%3a%2f%2fwww.google.com%2f // //escapeUrl := url.QueryEscape(urlStr) // //fmt.Println("编码:", escapeUrl) // //enEscapeUrl, _ := url.QueryUnescape(escapeUrl) // //fmt.Println("解码:", enEscapeUrl) // // // _ = c.Request.ParseForm() // req := c.Request.Form // debug := strings.Join(c.Request.Form["debug"], "") // sn := strings.Join(c.Request.Form["sn"], "") // ts := strings.Join(c.Request.Form["ts"], "") // // // 验证来源 // // AppSecret = "test" // // if debug == "1" { // var currentUnix int64 = 534243132 //自己写的 // req.Set("ts", strconv.FormatInt(currentUnix, 10)) // res := map[string]string{ // "ts": strconv.FormatInt(currentUnix, 10), // "sn": createSign(req), // } // return res, nil // } // abcd := "10" // // 验证过期时间 // timestamp := time.Now().Unix() // exp, _ := strconv.ParseInt(abcd, 10, 64) // tsInt, _ := strconv.ParseInt(ts, 10, 64) // if tsInt > timestamp || timestamp-tsInt >= exp {
// return nil, errors.New(“ts Error”)
// }
// // 验证签名
// if sn == “” || sn != createSign(req) {
// return nil, errors.New(“sn Error”)
// }
// return nil, nil
//}

// 创建签名
func createSign(params map[string]interface{}) (string, map[string]interface{}) {
params[“appSecret”] = AppSecret

_, haveSign := params[“sign”]
if haveSign {
delete(params, “sign”)
}

_, haveTimestamp := params[“timestamp”]
if !haveTimestamp {
params[“appSecret”] = time.Now().Unix()
}

sign := helper.Md5(helper.Md5(createEncryptChildStr(params)) + AppSecret)
delete(params, “appSecret”)

return sign, params
}

func createEncryptChildStr(params map[string]interface{}) string {
var key []string
var str = “”
for k := range params {
key = append(key, k)
}
sort.Strings(key)
for i := 0; i < len(key); i++ { val := params[key[i]] if i == 0 { str = fmt.Sprintf("%v=%v", key[i], val) } else { str = str + fmt.Sprintf("&%v=%v", key[i], val) } } return str }