2020-3-2lesson

Mar 2, 2020

Go、Go、Go

nodejs child_process 出现Stdout buffer issue using node child_process

使用child_process exec unzip.exe 出现解压缩包失败,全部文件错误字符打印了出来。

由于child_process 默认maxBuffer 是200KB,出现直接报Stdout buffer issue using node child_process

可以使用exec(command, {maxBuffer: 1024 * 500})

go build

1、Mac下编译Linux, Windows平台的64位可执行程序:

$ CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build test.go

$ CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build test.go

2、Linux下编译Mac, Windows平台的64位可执行程序:

$ CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 go build test.go

$ CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build test.go

3、Windows下编译Mac, Linux平台的64位可执行程序:

$ SET CGO_ENABLED=0 SET GOOS=darwin3 SET GOARCH=amd64 go build test.go

$ SET CGO_ENABLED=0 SET GOOS=linux SET GOARCH=amd64 go build test.go

或者:

1
2
3
4
5
6
7
8
9
10
export GOOS="windows"
go build -o phoneix-windows.exe

export GOOS="linux"
go build -o phoneix-linux

export GOOS="darwin"
go build -o phoneix-darwin

echo "done!"

golang 交叉斌编译CGO

当前使用go-sqlite3 库,无法直接Mac 打包成windows,报错: Binary was compiled with 'CGO_ENABLED=0', go-sqlite3 requires cgo to work. This is a stub

当前看到两篇文章使用两种方案:

  1. 使用docker 打包

    docker run --rm -v $GOPATH:/go golang:1.13 bash -c 'cd $GOPATH/src/ahuang.com/src/cobra/novel && CGO_ENABLE=1 GOOS=linux GOARCH=amd64 go build -o novel-linux'
    

链接

  1. brew 安装cross gcc 来打包
1
2
3
4
5
6
7
8
9
10
Windows平台(mingw-w64)
$ brew install mingw-w64
$ CGO_ENABLED=1 GOOS=windows GOARCH=amd64 CC=x86_64-w64-mingw32-gcc go build -v

Linux平台(x86_64-linux-musl-gcc)
$ brew install FiloSottile/musl-cross/musl-cross
$ CGO_ENABLED=1 GOOS=linux GOARCH=amd64 CC=x86_64-linux-musl-gcc CGO_LDFLAGS="-static" go build -a -v

-a: 重新编译
-static 标识静态链接,没有这个选项,linux上运行报: -bash: ./xxx: /lib/ld-musl-x86_64.so.1:bad ELF interpreter: No such file or directory

参考链接

开源 novel_terminal

开源了novel_terminal