Go环境搭建和GoLand安装

Go环境配置

1、下载go安装包

下载地址

根据需要选择下载的版本,这里我选择的是go.1.15
1、不推荐下载最新的go.1.18,因为可破解的goland是2020年版本,go语言版本升级IDEA版本不支持会导致报以下错误

‘main‘ collides with name declared in this package 或 Found several packages [http, main]

2、如果安装go的版本是1.17.11,golang是2020,由于版本不匹配无法在golang中自动识别出sdk

解决方法:打开GO的安装目录下的src\runtime\internal\sys\zversion.go文件,添加一行(我的go版本是1.17.11)
const TheVersion = go1.17.11 然后重启goland 这样再配置
goroot的时候就会自动识别到GO语言SDK版本了

在这里插入图片描述

自己选择安装路径,在cmd中go version检查是否安装成功

2、配置环境变量

安装完成后会自动在环境变量中添加了E:\Installation\Go\bin,若没有自动添加,则手动加上
在这里插入图片描述
接着在系统变量中新建GOROOT和GOPATH,其中GOROOT为go的安装路径,GOPATH是自己新建文件夹gowork的路径(在gowork中分别新建bin,src和pkg文件夹)
在这里插入图片描述

3、go env中修改配置

在这里插入图片描述
首先确保 GO111MODULE=on,可以在cmd中使用go env 查看,若没有打开,可以在命令行窗口通过

go env -w GO111MODULE=on

然后修改代理为阿里云,可以加快导入依赖的速度

go env -w GOPROXY=https://mirrors.aliyun.com/goproxy/

至此go环境安装完成

goland安装使用

1、官网下载goland安装包(推荐2020)

官网地址:GoLand
2020下载链接goland2020.03

2、安装到这一步选择对应的框框

在这里插入图片描述
选择第二个不导入配置
在这里插入图片描述

3、free方法

链接:https://pan.baidu.com/s/15kmt4wHnxAW2NDlP1qSr1A
提取码:zzz7

将该压缩包(不要解压)拖到gland代码区,重启goland
在这里插入图片描述选择help->eval reset
然后勾选 auto reset before per restart 那个选项,自勾选后每次重启/退出 IDE 时会自动重置试用信息,无需做额外的事情

在这里插入图片描述
在这里插入图片描述
——————————————————————————————————————————————————
我是分割线
——————————————————————————————————————————————————

创建go项目

1、用go mod搭建项目可以在任意位置新建文件bmsgo,在终端中执行命令

go mod init bmsgo

新建main.go文件
在vscode中打开该文件夹bmsgo
修改package main
执行go run main.go即可
(若项目中有多个go文件,
可以先通过 go build 生成 可执行文件,
然后./bmsgo来运行项目)

在goland中设置edit configurations

点击新建go build
run kind改为files
学习资料
go语言基础
goWeb
go中文文档

引入gin框架依赖

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

其中-u是指如果之前引入同一个依赖,则更新到最新的依赖
报错:

A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.

解决 :
确定配置好代理!go env -w GOPROXY=https://goproxy.cn,direct (不然依赖导入不进去),然后go env -w GOSUMDB=off
(或者换为阿里云代理)

go env -w GOPROXY=https://mirrors.aliyun.com/goproxy/

引入gorm框架依赖

gorm中文文档

go get -u github.com/jinzhu/gorm 

其中在gorm中,默认的表名都是结构体名称的复数形式,比如User结构体默认创建的表为users;**后面的json就是我们返回数组是前面对应的键

go get github.com/go-sql-driver/mysql

注意:在导入数据库 驱动时候,需要空白导入才可以

_"github.com/go-sql-driver/mysql"

引入jwt实现用户认证

go get github.com/dgrijalva/jwt-go

引入viper

go get github.com/spf13/viper

—————————————————————————————————

踩坑记录

遍历slice

for i:= range c{
	fmt.Printf("c[%d]:%d\n",i,c[i])
}

类型转换
string转成int:

int, err := strconv.Atoi(string)

string转成int64:

int64, err := strconv.ParseInt(string, 10, 64)

int转成string:

string := strconv.Itoa(int)

int64转成string:

string := strconv.FormatInt(int64,10)

gorm返回json数组形式

	var bookArr []model.Book
	var book model.Book

	result:=db.Find(&book)
	total:=result.RowsAffected

	db.Offset((page_num - 1) * page_size).Limit(pageSize).Find(&bookArr)

	sliceBook:=make([]model.Book,0)
	for i:=range bookArr{
		sliceBook=append(sliceBook,bookArr[i])
	}
	if sliceBook==nil{
		ctx.JSON(http.StatusOK,gin.H{"code":400,"msg":"no bookArr found"})
		return
	}
	response.Success(ctx,gin.H{"total":total, "book":sliceBook},"分页数据返回成功")

返回结果:

{
    "code": 200,
    "data": {
        "book": [
            {
                "ID": 3,
                "CreatedAt": "0001-01-01T00:00:00Z",
                "UpdatedAt": "0001-01-01T00:00:00Z",
                "DeletedAt": null,
                "bookName": "这就是二十四节气",
                "bookAuthor": "高春香",
                "bookPublish": "电子工业出版社"
            },
            {
                "ID": 4,
                "CreatedAt": "0001-01-01T00:00:00Z",
                "UpdatedAt": "0001-01-01T00:00:00Z",
                "DeletedAt": null,
                "bookName": "白夜行",
                "bookAuthor": "东野圭吾",
                "bookPublish": "南海出版公司"
            }
        ],
        "total": 14
    },
    "msg": "分页数据返回成功"
}

1、切片初始化参数、ctx获取前端数据格式

go插件

gocode	代码自动补全	https://github.com/mdempsky/gocode
go-outline	在当前文件中查找	https://github.com/ramya-rao-a/go-outline
go-symbols	在项目路径下查找	https://github.com/acroca/go-symbols
gopkgs	自动补全未导入包	https://github.com/uudashr/gopkgs
guru	查询所有引用	https://golang.org/x/tools/cmd/guru
gorename	重命名符号	https://golang.org/x/tools/cmd/gorename
goreturns	格式化代码	https://github.com/sqs/goreturns
godef	跳转到声明	https://github.com/rogpeppe/godef
godoc	鼠标悬浮时文档提示	https://golang.org/x/tools/cmd/godoc
golint	就是lint	https://golang.org/x/lint/golint
dlv	调试功能	https://github.com/derekparker/delve/tree/master/cmd/dlv
gomodifytags	修改结构体标签	https://github.com/fatih/gomodifytags
goplay	运行当前go文件	https://github.com/haya14busa/goplay/
impl	新建接口	https://github.com/josharian/impl
gotype-live	类型诊断	https://github.com/tylerb/gotype-live
gotests	单元测试	https://github.com/cweill/gotests/
go-langserver	语言服务	https://github.com/sourcegraph/go-langserver
filstruct	结构体成员默认值	https://github.com/davidrjenni/reftools/tree/master/cmd/fillstruct

  • 3
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

HSMgogogo!

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值