随笔分类 -  Golang

上一页 1 2 3 4 下一页

[Go] 获得一个整数范围区间的随机数 (golang)
摘要:示例:0,1 随机 package main import "fmt" import "math/rand" import "time" func main() { rand.Seed(time.Now().UnixNano()) fmt.Println(rand.Intn(2)) } 基于 mat 阅读全文

posted @ 2021-02-15 01:26 ercom 阅读(500) 评论(0) 推荐(0)

[Go] golang 去除 URI 链接中的 query string 参数
摘要:思路是使用 golang 的 net/url 包提供的方法解析url各部分,其中 URL.RawQuery 为查询参数部分,格式如 :a=b&c=d 然后我们再通过 strings.ReplaceAll 空字符替换掉这一部分即可。 我实现的例子:UriFilterExcludeQueryString 阅读全文

posted @ 2021-02-10 23:32 ercom 阅读(1795) 评论(0) 推荐(0)

[FAQ] Goland 始终没有包代码的提示 ?
摘要:表现:import 引入的包始终是红色的,表示没有找到引入的包。 注意,在这里开启Go Modules: 然后在 Exteneral Libraries 里看到 Go Modules 即可。 Refer:GoProxy Other:GoLand 需要手动开启代码补全吗 Link:https://ww 阅读全文

posted @ 2021-01-31 18:52 ercom 阅读(1546) 评论(0) 推荐(0)

[Go] godoc 打开本地文档, windows 同样适用
摘要:godoc 提供了在无网环境下 浏览官方文档的便利。 示例: $ go get golang.org/x/tools/cmd/godoc $ godoc -http=localhost:6060 Link:https://www.cnblogs.com/farwish/p/14239594.html 阅读全文

posted @ 2021-01-06 19:51 ercom 阅读(407) 评论(0) 推荐(0)

[FAQ] Golang error strings should not be capitalized or end with punctuation
摘要:当我们在 Golang 中使用 errors.New("Aaa.") 形式返回 error 信息时,文字内容不应该以大写字母开头或者标点符号结尾。 所以这样是可以的 errors.New("aaa") Refer:Golang常见错误 Link:https://www.cnblogs.com/far 阅读全文

posted @ 2021-01-03 20:15 ercom 阅读(3374) 评论(0) 推荐(0)

[Go] golang 替换组件包 更新 go.mod, go.sum 的方式
摘要:当我们不再使用某个包,或者包名变更时,是如何保证 go.mod 更新的。 只要代码中没有地方 import 使用到某个包了,我们运行: $ go mod tidy module 管理器会帮我们自动清理掉 go.mod, go.sum 中引入而未使用的包。 Refer:Golang组件包用法 Link 阅读全文

posted @ 2021-01-02 23:12 ercom 阅读(5989) 评论(0) 推荐(1)

[FAQ] panic: listen tcp :xxxx: bind: Only one usage of each socket address (protocol/network address/port) is normally permitted.
摘要:在 Go 中运行服务之前的绑定端口这一步,如果端口号被占用了,那么会提示它只能使用一次。 换个端口号或者检查端口号的占用程序。 Link:https://www.cnblogs.com/farwish/p/14194897.html 阅读全文

posted @ 2020-12-26 23:11 ercom 阅读(1955) 评论(0) 推荐(0)

[Go] golang 执行 Linux 系统 command
摘要:执行系统 shell 命令示例: fileDir := "files/"out, err := exec.Command("sh", "-c", "ls -alh " + fileDir).Output() if err != nil { panic("cmd error") } log.Print 阅读全文

posted @ 2020-09-19 19:23 ercom 阅读(571) 评论(0) 推荐(0)

[Go] panic: assignment to entry in nil map
摘要:以上错误出现在给 map 变量赋值的时候。 例如: type AbMap map[string]string var abMap AbMap abMap['a'] = 'b' 使用 map 变量需要使用 make 初始化,然后才能赋值。 type AbMap map[string]string ab 阅读全文

posted @ 2020-09-15 22:33 ercom 阅读(1660) 评论(0) 推荐(0)

Golang 与 JS 的字符串截取大同小异
摘要:Golang 和 JS 的字符串截取都可以利用索引定位的方式。 Golang: str := "abcdef" sub := str[1: 2] JS: const str = 'abcdef' const sub = str.substring(1, 2) Link:https://www.cnb 阅读全文

posted @ 2020-07-29 22:41 ercom 阅读(276) 评论(0) 推荐(0)

[Go] golang 两个数组 list 的合并方式
摘要:s := append([]int{1, 2}, []int{3, 4}...) Tool:在线Golang代码运行 Cool:在线 AI 编程助手 https://stackoverflow.com/questions/16248241/concatenate-two-slices-in-go L 阅读全文

posted @ 2020-06-06 17:38 ercom 阅读(9751) 评论(0) 推荐(0)

[Go] assignment count mismatch 1 = 2
摘要:Golang 中这个错误的的意思是赋值变量的数目不匹配。 举例: result := json.Marshal(List) 由于没有给返回值中的 error 正确赋值,就会报 assignment count mismatch 1 = 2 正确写法: result, _ := json.Marsha 阅读全文

posted @ 2020-06-04 21:22 ercom 阅读(3479) 评论(0) 推荐(0)

[Go] golang 时间格式化 12小时制 与 24小时制
摘要:timestamp := int64(1591271169) # 12小时制 time.Unix(timestamp, 0).Format("2006-01-02 03:04:05") # 24小时制 time.Unix(timestamp, 0).Format("2006-01-02 15:04: 阅读全文

posted @ 2020-06-04 19:56 ercom 阅读(2039) 评论(0) 推荐(0)

[Go] 让 go build 生成的可执行文件对 Mac、linux、Windows 平台一致
摘要:要做到这一点,使用的是交叉编译选项。 CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build main.go CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build main.go CGO_ENABLED=0 GOOS= 阅读全文

posted @ 2020-06-01 09:35 ercom 阅读(4788) 评论(0) 推荐(0)

[Go] go build 减小二进制文件大小的几种方式
摘要:第一种 是去除不需要的调试信息: go build -ldflags "-s -w" main.go 实测 19M 减小为 15M,幅度 2% 第二种 压缩 UPX: the Ultimate Packer for eXecutables 第三种 更新至 go1.15 版本,See here. Re 阅读全文

posted @ 2020-05-30 19:43 ercom 阅读(1684) 评论(0) 推荐(0)

[Go] CORS 支持多个 origin 访问的思路 (Access-Control-Allow-Origin 部分)
摘要:以下为局部伪代码,仅供参考: var allowOrigin string allowOrigins := config.AppConf.Get("middleware.cors.allowOrigins").(string) if strings.Contains(allowOrigins, "* 阅读全文

posted @ 2020-05-29 15:24 ercom 阅读(1729) 评论(0) 推荐(0)

[FAQ] Error occured while trying to proxy to: xx.xx.x.xx:xx/xx
摘要:遇到这种情况,要知道证明访问并未到达指定的服务地址。 可能原因有未启动、端口占用 等等,请逐一排查。 Tool:ChatAI Refer:Proxy_Error Link:https://www.cnblogs.com/farwish/p/12966391.html 阅读全文

posted @ 2020-05-26 16:30 ercom 阅读(4912) 评论(0) 推荐(0)

[Go] httprouter 自动 OPTIONS 响应 和 CORS
摘要:httprouter 是 Gin framework 使用的路由组件。 要对 OPTIONS 请求自动响应,比如支持 CORS 请求或者设置请求头,可用 Router.GlobalOPTIONS。 router.GlobalOPTIONS = http.HandlerFunc(func(w http 阅读全文

posted @ 2020-05-19 15:11 ercom 阅读(944) 评论(0) 推荐(0)

[Go] 结构体成员的第三个位置上标签的作用
摘要:结构体成员加了第三个位置的标签,在转换指定类型时,key 会使用指定的名字。 package main import ( "encoding/json" "log" ) type Person struct { Age int FirstName string LastName string `js 阅读全文

posted @ 2020-05-19 14:58 ercom 阅读(411) 评论(0) 推荐(0)

[Go] 结构体 嵌套 结构体指针 的含义
摘要:举个例子:以下 FutureKline 这个结构体 包含了 Kline 结构体的指针,为什么不直接是 Kline 结构体。 type Kline struct { Pair CurrencyPair Low float64 Vol float64 } type FutureKline struct 阅读全文

posted @ 2020-05-19 14:12 ercom 阅读(1906) 评论(0) 推荐(0)

上一页 1 2 3 4 下一页