go引入外部依赖的三种方式:go get,go module,vendor目录:https://blog.csdn.net/hyzx_9987/article/details/103962611
六款最棒的 Go 语言 Web 框架简介:https://www.cnblogs.com/tomyyyyy/articles/12900961.html
问题一:go get github.com/astaxie/beego 没有反应
解决办法:https://blog.csdn.net/zw421152835/article/details/102532675 转载
#问题点大致是明确的:网络问题不能访问。
#通过命令 go env 发现,有一个GOPROXY,我们知道npm maven 等都可以设置国内镜像/库地址的,那这个能不能了?当然是可以的。
#https://github.com/goproxy/goproxy.cn/blob/master/README.zh-CN.md 有介绍
#执行:
go env -w GOPROXY=https://goproxy.cn,direct
问题二:beego+swagger自动生成文档失败。
#输入命令:
bee run -gendoc=true -downdoc=true
#报错:
ERROR ▶ 0005 Failed to generate the docs.
解题思路:报错原因不详细,需要查找到错误原因。bee run为程序运行命令,gendoc参数生成文档,downdoc参数自动重建文档。需要找到只是生成文档的命令。
解决办法:https://studygolang.com/articles/4271 转载
#输入生成文档命令:
bee generate docs
#出现详细报错信息:
xxcontroller param must have four balabalabala......
#按注解路由要求写好注释,重新运行,运行成功。^_^
#swagger页面地址为:
http://127.0.0.1:8080/swagger/#!/
问题三:GOROOT、GOPATH相关
解决办法:https://www.jianshu.com/p/4e18cf7f0b70 转载
go env # 查看设置的参数
go version # 查看安装的 Go 的版本
bee version # 查看beego的版本
# GOROOT为安装GO的目录。GOPATH是工程目录。
# GO下载后,会自动在user/xx用户名 下面自动生成工程文件夹。
# 为了防止xx用户名中包含中文,对开发过程有影响,可以移动GOPATH。
# 【注意】然后一定要在系统环境变量中更改GOPATH路径!!!
问题四:修改beego+swagger自动生成文档方式
知识准备:
(1)注解参数:
https://ithelp.ithome.com.tw/articles/10224114 转载 注解参数更全一些
(2)解析json:想要解析json,或者预先定义结构体,或者解析成map一层一层剖出来(map中的索引的值不可寻址)。就很难受...........
https://blog.csdn.net/lengyuezuixue/article/details/78771754 转载 go语言解析json,使用Marshal和Unmarshal对json数据格式进行编码和解码。
https://blog.csdn.net/xiaoquantouer/article/details/80233177?utm_medium=distribute.pc_relevant.none-task-blog-baidujs_title-2&spm=1001.2101.3001.4242 Marshal和Unmarshal使用实例
https://github.com/buger/jsonparser 转载 git上的据说不用预先定义结构的办法,没仔细看。
https://www.jb51.net/article/148350.htm 转载 json-inteator,是目前golang中对json格式数据处理最快的包(比官方json包快6倍)。
(3)类型断言:
https://studygolang.com/articles/14269 转载 获取变量数据类型
https://studygolang.com/articles/21591?fr=sidebar 转载 类型转换
https://studygolang.com/articles/19455 转载 类型断言取不到地址
解决思路:直接修改swagger.json文件,设置paramter的default。由于default无法传双引号,先传单引号,然后替换成双引号,进行json格式化,写回swagger.json文件。思路比较粗暴潦草...治标不治本......
解决办法:代码如下,GenDoc方法在beego的main.go中生成文档后使用。
//定义几个类型方便使用
type Dict = map[interface {}]interface {}
type SDict = map[string]interface {}
type List = []interface {}
//比较粗糙的解析map,用处不大可以不用看。
func respHandler(res interface{}) (tmp map[string]interface{}) {
// map 需要初始化一个出来
tmp = make(map[string]interface{})
//log.Println("input res is : ", res)
switch res.(type) {
case nil:
return tmp
case SDict:
return res.(SDict)
case Dict:
//log.Println("map[interface{}]interface{} res:", res)
for k, v := range res.(Dict) {
log.Println("loop:", k, v)
switch k.(type) {
case string:
switch v.(type) {
case Dict:
//log.Println("map[interface{}]interface{} v:", v)
tmp[k.(string)] = respHandler(v)
continue
default:
//log.Printf("default v: %v %v \n", k, v)
tmp[k.(string)] = v
}
default:
continue
}
}
return tmp
default:
// 暂时没遇到更复杂的数据
//log.Println("unknow data:", res)
}
return tmp
}
//修改swagger.json
func GenDoc(fileName string) {
doc := ReadFile(fileName)
a := make(SDict)
json.Unmarshal([]byte(doc), &a)
m := respHandler(a)
for _, route := range m["paths"].(SDict) {
for method, method_in := range route.(SDict) {
if method == "post" {
defaultD := method_in.(SDict)["parameters"].(List)[0].(SDict)
defaultParam := defaultD["default"]
if defaultParam != nil {
var jsonNewDefaultParam bytes.Buffer
json.Indent(&jsonNewDefaultParam, []byte(strings.Replace(defaultParam.(string), "'", "\"", -1)), "", " ")
defaultD["default"] = jsonNewDefaultParam.String()
}
}
}
}
newDoc, _ := json.Marshal(m)
WriteString(fileName, string(newDoc))
}
问题五:路径的斜杠“/” 会变成 “%2F”
解决方法:https://www.52bd.net/code/53.html 转载
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)
}