Skip to content

Commit 95798d7

Browse files
committed
update 2022年10月14日 星期五 17时54分30秒 CST
1 parent 767fb02 commit 95798d7

File tree

99 files changed

+4120
-1196
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

99 files changed

+4120
-1196
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ push.sh
88
!/assets/img/Develop/Python
99
/1earn/Develop/Python/基础
1010
/assets/img/Develop/Python/基础
11+
!/1earn/Develop/Golang
12+
/1earn/Develop/Golang/基础
1113
!/1earn/Develop/计算机基础
1214
!/assets/img/Develop/计算机基础
1315
!/1earn/Develop/Code.md

1earn/Develop/Golang/GO.md

+439
Large diffs are not rendered by default.
+95
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
# GoReleaser
2+
3+
- https://github.com/goreleaser/goreleaser
4+
5+
---
6+
7+
自动化打包工具
8+
9+
---
10+
11+
**安装**
12+
13+
- https://github.com/goreleaser/goreleaser/releases
14+
15+
---
16+
17+
**使用**
18+
19+
在目标目录执行 goreleaser init ,将会生成一个 `.goreleaser.yml` 配置文件
20+
21+
修改配置,例如
22+
```yml
23+
# This is an example .goreleaser.yml file with some sane defaults.
24+
# Make sure to check the documentation at http://goreleaser.com
25+
before:
26+
hooks:
27+
# You may remove this if you don't use go modules.
28+
#- go mod tidy
29+
# you may remove this if you don't need go generate
30+
#- go generate ./...
31+
builds:
32+
- env:
33+
- CGO_ENABLED=0
34+
id: "anewproject"
35+
binary: "anew"
36+
goos:
37+
- linux
38+
- windows
39+
- darwin
40+
goarch:
41+
- amd64
42+
archives:
43+
- replacements:
44+
darwin: Darwin
45+
linux: Linux
46+
windows: Windows
47+
386: i386
48+
amd64: x86_64
49+
checksum:
50+
name_template: 'checksums.txt'
51+
snapshot:
52+
name_template: "v1.0.0-snapshot"
53+
changelog:
54+
sort: asc
55+
filters:
56+
exclude:
57+
- '^docs:'
58+
- '^test:'
59+
```
60+
61+
**打包**
62+
63+
```
64+
goreleaser --snapshot --skip-publish --rm-dist
65+
```
66+
67+
这里可能会提示
68+
69+
```
70+
⨯ release failed after 2.94s error=failed to build for darwin_amd64: go: cannot find main module, but found .git/config in C:\xxx
71+
```
72+
73+
直接
74+
```
75+
go mod init hello
76+
```
77+
再运行一遍就可以了
78+
79+
如果提示一下报错
80+
```
81+
couldn't guess project_name, please add it to your config
82+
```
83+
84+
添加一行就行
85+
```yaml
86+
project_name: myproject
87+
```
88+
89+
**main.go 不在根目录的情况**
90+
91+
```yaml
92+
# .goreleaser.yaml
93+
builds:
94+
- main: ./path/to/your/main/pkg/
95+
```
+145
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
# flag 包
2+
3+
---
4+
5+
**Source & Reference**
6+
7+
- [Go语言flag包:命令行参数解析](http://c.biancheng.net/view/5573.html)
8+
9+
---
10+
11+
几个概念:
12+
- 命令行参数(或参数):是指运行程序提供的参数
13+
- 已定义命令行参数:是指程序中通过 flag.Xxx 等这种形式定义了的参数
14+
- 非 flag(non-flag)命令行参数(或保留的命令行参数):先可以简单理解为 flag 包不能解析的参数
15+
16+
```go
17+
package main
18+
19+
import (
20+
"flag"
21+
"fmt"
22+
"os"
23+
)
24+
25+
var (
26+
h, H bool
27+
28+
v bool
29+
q *bool
30+
31+
D string
32+
Conf string
33+
)
34+
35+
func init() {
36+
flag.BoolVar(&h, "h", false, "帮助信息")
37+
flag.BoolVar(&h, "H", false, "帮助信息")
38+
39+
flag.BoolVar(&v, "v", false, "显示版本号")
40+
41+
//
42+
flag.StringVar(&D, "D", "deamon", "set descripton ")
43+
flag.StringVar(&Conf, "Conf", "/dev/conf/cli.conf", "set Conf filename ")
44+
45+
// 另一种绑定方式
46+
q = flag.Bool("q", false, "退出程序")
47+
48+
// 像flag.Xxx函数格式都是一样的,第一个参数表示参数名称,
49+
// 第二个参数表示默认值,第三个参数表示使用说明和描述.
50+
// flag.XxxVar这样的函数第一个参数换成了变量地址,
51+
// 后面的参数和flag.Xxx是一样的.
52+
53+
// 改变默认的 Usage
54+
55+
flag.Usage = usage
56+
57+
flag.Parse()
58+
59+
var cmd string = flag.Arg(0)
60+
61+
fmt.Printf("-----------------------\n")
62+
fmt.Printf("cli non=flags : %s\n", cmd)
63+
64+
fmt.Printf("q: %b\n", *q)
65+
66+
fmt.Printf("descripton: %s\n", D)
67+
fmt.Printf("Conf filename : %s\n", Conf)
68+
69+
fmt.Printf("-----------------------\n")
70+
fmt.Printf("there are %d non-flag input param\n", flag.NArg())
71+
for i, param := range flag.Args() {
72+
fmt.Printf("#%d :%s\n", i, param)
73+
}
74+
75+
}
76+
77+
func main() {
78+
flag.Parse()
79+
80+
if h || H {
81+
flag.Usage()
82+
}
83+
}
84+
85+
func usage() {
86+
fmt.Fprintf(os.Stderr, `CLI: 8.0
87+
Usage: Cli [-hvq] [-D descripton] [-Conf filename]
88+
89+
`)
90+
flag.PrintDefaults()
91+
}
92+
```
93+
94+
flag 包实现了命令行参数的解析,大致需要几个步骤:
95+
96+
**flag 参数定义或绑定**
97+
98+
定义 flags 有两种方式:
99+
100+
1. flag.Xxx(),其中 Xxx 可以是 Int、String 等;返回一个相应类型的指针,如:
101+
```go
102+
var ip = flag.Int("flagname", 1234, "help message for flagname")
103+
```
104+
105+
2. flag.XxxVar(),将 flag 绑定到一个变量上,如:
106+
```go
107+
var flagvar int
108+
flag.IntVar(&flagvar, "flagname", 1234, "help message for flagname")
109+
```
110+
另外,还可以创建自定义 flag,只要实现 flag.Value 接口即可(要求 receiver 是指针),这时候可以通过如下方式定义该 flag:
111+
```go
112+
flag.Var(&flagVal, "name", "help message for flagname")
113+
```
114+
命令行 flag 的语法有如下三种形式:-flag // 只支持 bool 类型 -flag=x -flag x // 只支持非 bool 类型
115+
116+
**flag 参数解析**
117+
118+
在所有的 flag 定义完成之后,可以通过调用 `flag.Parse()` 进行解析.
119+
120+
根据 `Parse()` 中 for 循环终止的条件,当 parseOne 返回 false,nil 时,Parse 解析终止.
121+
```go
122+
s := f.args[0]
123+
if len(s) == 0 || s[0] != '-' || len(s) == 1 {
124+
return false, nil
125+
}
126+
```
127+
128+
当遇到单独的一个"-"或不是"-"开始时,会停止解析.比如:./cli - -f 或 ./cli -f,这两种情况,-f 都不会被正确解析.像这些参数,我们称之为 non-flag 参数.
129+
130+
parseOne 方法中接下来是处理 -flag=x,然后是 -flag(bool 类型)(这里对 bool 进行了特殊处理),接着是 -flag x 这种形式,最后,将解析成功的 Flag 实例存入 FlagSet 的 actual map 中.
131+
132+
`Arg(i int)``Args()``NArg()``NFlag()` `Arg(i int)``Args()` 这两个方法就是获取 non-flag 参数的;`NArg()` 获得 non-flag 个数;`NFlag()` 获得 FlagSet 中 actual 长度(即被设置了的参数个数).
133+
134+
flag 解析遇到 non-flag 参数就停止了.所以如果我们将 non-flag 参数放在最前面,flag 什么也不会解析,因为 flag 遇到了这个就停止解析了.
135+
136+
**分支程序**
137+
138+
根据参数值,代码进入分支程序,执行相关功能.上面代码提供了 -h 参数的功能执行.
139+
```go
140+
if h || H {
141+
flag.Usage()
142+
}
143+
```
144+
145+
总体而言,从例子上看,flag package 很有用,但是并没有强大到解析一切的程度.如果入参解析非常复杂,flag 可能捉襟见肘.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# goland 远程调试
2+
3+
---
4+
5+
本地的gopath和远端机器不需要一致,go版本也不需要一致
6+
7+
---
8+
9+
## 远端机器
10+
11+
**go**
12+
13+
需要安装 go 环境,可以用 f8x 装
14+
```bash
15+
wget -O f8x https://f8x.io/ && mv --force f8x /usr/local/bin/f8x && chmod +x /usr/local/bin/f8x
16+
f8x -go
17+
```
18+
19+
**dlv**
20+
21+
装一个delve
22+
23+
```bash
24+
export GO111MODULE=on && export GOPROXY=https://goproxy.io
25+
go install -v github.com/go-delve/delve/cmd/dlv@latest
26+
which dlv
27+
ln -s /root/go/bin/dlv /usr/local/bin/dlv
28+
dlv
29+
```
30+
31+
---
32+
33+
## 本地
34+
35+
检测下 goland 是否自带 `FTP/SFTP/WebDAV Connectivity` 插件
36+
37+
**添加远程服务器信息**
38+
39+
点击tools->deployment->configuration
40+
41+
添加一个 sftp
42+
43+
添加远程服务器,添加映射
44+
45+
**测试上传**
46+
47+
tools->deployment->upload to
48+
49+
**开始调试**
50+
51+
右上角的配置,选择go remote
52+
53+
配置远程服务器信息,端口使用默认
54+
55+
会给出远端需要运行的命令
56+
```bash
57+
dlv debug --headless --listen=:2345 --api-version=2
58+
```
59+
60+
远端运行后,即可正常调试

0 commit comments

Comments
 (0)