65.9K
CodeProject 正在变化。 阅读更多。
Home

Gos:武装的 Golang

starIconstarIconstarIconstarIconstarIcon

5.00/5 (2投票s)

2019年5月21日

CPOL

2分钟阅读

viewsIcon

37623

gos 与所有 go 命令兼容,并配备了智能 GOPROXY 的 go mod/get,它能自动区分私有和公共仓库,并在适当的时候使用 GOPROXY 下载您丢失的包。

Gos:武装的 Golang 💪

CircleCI Go Report Card Build Status GoDoc Gitter chat

项目地址:https://github.com/storyicon/gos

gos

当前的 gos 仍然是 alpha 版本,欢迎更多人评论和改进它 🍓,您可以添加更多命令,或者修改某些内容以使其性能更好。

您可以在这里下载编译后的二进制程序:发布页面

🦄 简要介绍

从现在开始,使用 gos 代替 go

go get => gos get
go build => gos build
go run => gos run
go ... => gos ...

gos 与所有 go 命令兼容,并配备了智能 GOPROXY 的 go mod/get,它能自动区分私有和公共仓库,并在适当的时候使用 GOPROXY 下载您丢失的包。

gos 还有一些额外的命令来增强您的开发体验

  cross      agile and fast cross compiling
  proto      quick and easy compilation of proto files

您可以使用 -h 在这些子命令上获取更多信息。

🐋 如何开始

这再简单不过了。
根据您的系统类型,从 发布页面 下载 zip 文件,解压缩,将二进制文件重命名为 gos 并将其放入您的 $PATH 中。然后像使用 go 命令一样使用 gos
您也可以下载源代码并使用 go build -o gos main.go 进行编译

注意: gos 正常工作的前提是 go 二进制文件 位于您的 $PATH 中。如果您需要使用 gos proto 命令,您还需要 protoc 二进制文件

🍊 GOS 能做什么

1. 完全兼容 Go 原生命令

您可以像使用 go 命令一样使用 gos。兼容所有标志和参数,例如以下

go get -u -v github.com/xxxx/xxxx
=>
gos get -u -v github.com/xxxx/xxxx

2. 更简单的交叉编译

您可以使用 gos cross 命令进行更简单的交叉编译

# Compile Linux platform binaries for the current system architecture
# For example, if your computer are amd64, 
# it will compile main.go into the binary of linux/amd64 architecture.
gos cross main.go linux

# Specify the build platform and architecture
gos cross main.go linux amd64
gos cross main.go linux arm
gos cross main.go linux 386
gos cross main.go windows amd64
gos cross main.go darwin 386

# Compiling binary files for all architectures on the specified platform
gos cross main.go linux all
gos cross main.go windows all

# Compiling binary files for all platforms on the specified architecture
gos cross main.go all amd64

# Trying to compile binary files for all platforms and architectures
gos cross all all

Gos 使用并行编译,速度非常快 🚀,但仍然取决于您操作系统的配置。

更多信息:gos cross -h

3. 快速生成 .proto

此功能可能仅对 RPC 开发人员有用。您可以更轻松地编译 proto 文件,如下所示

# Compile a single file
gos proto helloworld.proto

# Compile all proto files under the current folder (excluding subfolders)
gos proto all

# Compile all proto files in the current directory and all subdirectories
gos proto all/all

当然,前提条件是您在 $PATH 中有一个 protoc 二进制文件

更多信息:gos proto -h

4. Go 代理解决方案

这里存在一个困境。如果您不使用 GOPROXY,可能会出现大量包拉取超时(网络原因)或不存在(仓库重命名、删除或迁移),如下所示

unrecognized import path "golang.org/x/net" (https fetch: Get https://golang.ac.cn/x/net?go-get=1: 
dial tcp 216.239.37.1:443: connectex: 
A connection attempt failed because the connected party did not properly respond 
after a period of time, or established connection failed because connected host 
has failed to respond.)

如果使用 GOPROXY,您将无法正确拉取私有仓库(github、gitlab 等),如下所示

go get github.com/your_private_repo: unexpected status 
(https://athens.azurefd.net/github.com/your_private_repo/@v/list): 500 Internal Server Error

GOS 增强了所有 GO 的原生命令,无论它是 go mod/get/build/run/... 任何可能导致包拉取的场景,gos 都会智能地确定当前要拉取的仓库是否需要使用 GOPROXY

现在,尽情享受你的生活吧。😎

© . All rights reserved.