wsl上安装
下载
根据自己的版本下载
https://go.dev/dl/
我下载了go1.22.4.linux-amd64.tar.gz
解压到指定目录
sudo tar -C /usr/local -xzf go1.XX.X.linux-amd64.tar.gz
修改环境变量
编辑~/.profile 或 ~/.bashrc 文件
添加如下内容
export PATH=$PATH:/usr/local/go/bin
export GOPATH=$HOME/go
export PATH=$PATH:$GOPATH/bin
让环境变量命令即使生效,使用
source ~/.profile
验证
go version
gin cors
有个项目的cors设置如下,是前后端分离项目,用的好好的,所以新项目的时候直接用了这个配置,发现不太行,一直报cors问题
r.Use(cors.New(cors.Config{
AllowOrigins: []string{"*"}, // 或者指定允许的域名,例:"http://example.com"
AllowMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"},
AllowHeaders: []string{"Origin", "Content-Type", "Authorization"},
ExposeHeaders: []string{"Content-Length"},
AllowCredentials: true,
MaxAge: 12 * time.Hour,
}))
找资料:当你设置了 AllowCredentials: true 时,CORS 的配置不能允许 "" 作为 AllowOrigins,因为浏览器会认为你不允许跨域请求带上凭证(如 cookies 或 HTTP 认证信息)。根据 CORS 规范,当 AllowCredentials 为 true 时,AllowOrigins 必须是明确指定的域名,不能是 。
改成如下,指定了域名
r := gin.Default()
r.Use(cors.New(cors.Config{
AllowOrigins: []string{"http://localhost:3000"},
AllowMethods: []string{"GET", "POST", "PUT", "DELETE", "OPTIONS"},
AllowHeaders: []string{"Origin", "Content-Type", "Authorization"},
ExposeHeaders: []string{"Content-Length"},
AllowCredentials: true,
MaxAge: 12 * time.Hour,
}))
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。