Skip to content

Commit

Permalink
go
Browse files Browse the repository at this point in the history
  • Loading branch information
ahuigo committed May 29, 2020
1 parent 6cd2f5c commit 6e23db7
Show file tree
Hide file tree
Showing 28 changed files with 759 additions and 34 deletions.
2 changes: 1 addition & 1 deletion post/arch/docker-image-.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ docker import理解为将外部文件复制进来形成只有一层文件系统
COPY . /app
WORKDIR /app
RUN npm install --registry=https://registry.npm.taobao.org
EXPOSE 3000
EXPOSE 3000 3002
FROM centos:6.7
MAINTAINER Fisher "fisher@sudops.com"

Expand Down
2 changes: 1 addition & 1 deletion post/c/ops-file-watch.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,5 +70,5 @@ inotify的主要应用于桌面搜索软件,像:Beagle,得以针对有变
其中:

-r recursive
-e MODIFY
-e MODIFY DELETE 等事件
-m path specify the path that we monitor
1 change: 1 addition & 0 deletions post/c/1.shell-var.md → post/c/shell-var.md
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ substr is beginning:
#### substr 截取

echo ${str:start:length}
echo ${str:start} #start 不为负数.否则当作0
echo ${str: -1}; #负数前面需要有空格
echo ${str:(-1)}; #或者负数用括号. 否则负数不会生效

Expand Down
1 change: 0 additions & 1 deletion post/db/mysql-index.md
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,6 @@ B树数据库充分利用了磁盘块(4K)的原理
那可能有人会问,这个比例有什么经验值吗?使用场景不同,这个值也很难确定,一般需要join的字段我们都要求是0.1以上,即平均1条扫描10条记录
4. 索引列不能参与计算,保持列“干净”,比如from_unixtime(create_time) = ’2014-05-29’就不能使用到索引,原因很简单,b+树中存的都是数据表中的字段值,但进行检索时,需要把所有元素都应用函数才能比较,显然成本太大。
所以语句应该写成create_time = unix_timestamp(’2014-05-29’);
5. 尽量的扩展索引,不要新建索引。比如表中已经有a的索引,现在要加(a,b)的索引,那么只需要修改原来的索引即可

## 对于准备要索引的列必须使用NOT NULL
这样就不会存储NULL(含NULL 的列区分度低), 而是默认值。
Expand Down
5 changes: 5 additions & 0 deletions post/db/pg-ddl-install.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ see db-user.md
# auto reboot
sudo systemctl enable postgresql
elif osx:
# 手动
# To have launchd start postgresql now and restart at login:
brew services start postgresql

Expand All @@ -50,6 +51,10 @@ see db-user.md
alias pg-stop="launchctl unload ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist"
MM

然后登录:

psql -U role1 ahuigo

## debug

ps aux| grep postgres
Expand Down
1 change: 1 addition & 0 deletions post/db/pg-ddl-user.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ password method 使用独立的帐号,使用ROLE管理
psql -U user_name -d database_name -h 127.0.0.1 -W
\W prompt enter password

## user login
Non interactive password:

1. vim ~/.pgpass:
Expand Down
5 changes: 5 additions & 0 deletions post/go/15.go-test.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ Go has a lightweight test framework composed of the go test command and the test
超时
-bench regexp 执行相应的 benchmarks,例如
-bench "Benchmark_*"
-bench=. ; 表示运行所有基准测试
-bench=Alloc ; 表示运行所有基准测试: Benchmark_Alloc


## unit test
github.com/ahuigo/go-lib/gotest
Expand Down Expand Up @@ -130,6 +133,8 @@ Error/Fatal 都会导致bench 不被执行
通过-benchtime参数可以自定义测试时间,例如:

$ go test -v -bench=. -benchtime=5ms bench_test.go
5ms
5s

### count重复执行
如果想定义执行5次(实际次数是b.N*5)
Expand Down
67 changes: 65 additions & 2 deletions post/go/go-array-slice.md
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ len(src)>len(dst) 会被golang 截断

新分配空间 append 会copy 并扩容

s2 = append(s,1,2) //s!=s2 (完全不同的内存区域)
s2 = append(s,1,2) //s!=s2 (如果是新分配空间,则是完全不同的内存区域)

如果不分配空间就用:

Expand Down Expand Up @@ -249,4 +249,67 @@ Since a slice doesn't make a copy of the underlying array. To decrease memory m
c := make([]byte, len(b))
copy(c, b)
return c
}
}

# copy and deepcopy
https://flaviocopes.com/go-copying-structs/

## 浅copy
见go-lib/struct/copy-*.go

### slice 浅copy
slice copy:

copy(s_dst, s_src)

e.g:

type Cat struct {
age int
name string
friends []string
}

func main() {
wilson := []Cat{{7, "Wilson", []string{"Tom", "Tabata", "Willie"}}}
nikita := []Cat{{}}
copy(nikita, wilson) //Cat是浅复制
nikita[0].age=6
nikita[0].friends[0]="newuser" //friends 同时改变

fmt.Println(wilson) //[{7 Wilson [newuser Tabata Willie]}]
fmt.Println(nikita) //[{6 Wilson [newuser Tabata Willie]}]
}

### assign 浅copy
s_dst := s_src[:]
s_dst := s_src

与copy slice 一样的结果

wilson := Cat{7, "Wilson", []string{"Tom", "Tabata", "Willie"}}
nikita := wilson //浅copy Cat
nikita.age = 6
nikita.friends[0]= "newuser"

## append 浅copy

cat:= Cat{7, "Wilson", []string{"Tom", "Tabata", "Willie"}}
s := append([]Cat{}, cat)
s[0].age=6
s[0].friends[0]="newuser"
fmt.Println(s) //[{6 Wilson [newuser Tabata Willie]}]


## deepcopy
深复制的方法:利用copier+新对象

### copier+new Class

// "github.com/jinzhu/copier"
wilson := Cat{7, "Wilson", []string{"Tom", "Tabata", "Willie"}}
nikita := Cat{}
copier.Copy(&nikita, &wilson)

### copier+make
nikita.friends = make([]string, len(wilson.friends)) //指针替换
93 changes: 88 additions & 5 deletions post/go/go-debug.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,92 @@ title: Golang: debuging & Live Reload with docker
date: 2020-04-01
private: true
---
# Golang: debuging & Live Reload with docker
> refer to https://medium.com/@hananrok/debugging-hot-reloading-go-app-within-docker-container-b44d2929e8bd
> code example: go-lib/go-debug/
# debug: vscode + hotReload + docker + dlv
> refer1:livereload https://medium.com/@hananrok/debugging-hot-reloading-go-app-within-docker-container-b44d2929e8bd
> refer2:vscode https://bloggie.io/@_ChristineOo/debugging-go-with-delve-and-vscode
code example: go-lib/debug/docker/main.go

## Prepare 准备
1. 项目代码,比如go-lib/debug/main.go
## 利用ionotify+目录挂载实现livereload
首先将当前的源码映射到一个目录,如/build 或/app/src:

docker run --rm --name server8 -v $PWD:/build -p8080:8080 -p2345:2345 delve-docker-vscode-example

再利用sh run.sh 写一个liverreload 脚本

## 利用delve 进行debugger
Delve is full featured debugging tool for Go.

go get github.com/go-delve/delve/cmd/dlv

### dlv 可执行文件
想使用delve, 需要编译支持:

# cgo 关闭动态态链接
ENV CGO_ENABLED 0

# 加:`-N -l` 去掉optimiztion 和inlining 内联
RUN go -gcflags "all=-N -l" build -o /server main.go

然后使用delve 开一个监听

dlv --listen=:2345 --headless=true --api-version=2 exec ./server
dlv --listen=:2345 --headless=true --api-version=2 --log exec ./server
dlv --listen=:2345 --headless=true --api-version=2 --accept-multiclient exec ./server

### dev debug .go

dlv debug --headless --listen=:2345 --api-version=2 --log main.go

## 利用docker 执行
本例我们使用docker执行 dlv:

docker run --rm --name server8 -v $PWD:/build -p8080:8080 -p2345:2345 --security-opt=seccomp:unconfined delve-docker-vscode-example
API server listening at: [::]:2345

`--security-opt=seccomp:unconfined` 是必须的, 因为:

Docker has security settings preventing ptrace(2) operations by default with in the container. Pass --security-opt=seccomp:unconfined

如果是在docker-compose.yml 中执行参考:https://github.com/christineoo/go-delve-docker-vscode-example/blob/master/docker-compose.yml

services:
web:
container_name: go-delve-docker-vscode-example
build: "./"
ports:
- "8080:8080"
- "2345:2345"
security_opt:
- "seccomp:unconfined"
tty: true
stdin_open: true
command: dlv debug --headless --listen=:2345 --api-version=2 --log main.go

## vscode 连接delve
配置dlv: localhost:2345, 指定`remotePath:"/go/src"`

launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "Launch",
"type": "go",
"request": "launch",
"mode": "remote",
"program": "${workspaceFolder}", //本地源
"remotePath": "/go/src",
"port": 2345,
"env": {},
"args": [],
"showLog": true,
"trace": "verbose"
}
]
}

启动项目执行断点
1. vscode: main.go 加断点
2. 启动docker 或 docker-compose 或者纯dlv
3. vscode: F5启用 Debug: start
4. 访问8080 服务
Loading

0 comments on commit 6e23db7

Please sign in to comment.