分类目录归档:Golang

golang交叉编译

Windows主机编译Linux,MAC客户端

Windows主机编译Windows客户端

SET CGO_ENABLED=0
SET GOOS=windows
SET GOARCH=amd64
go build -o client-windows.exe main.go

Windows主机编译LINUX客户端

SET CGO_ENABLED=0
SET GOOS=linux
SET GOARCH=amd64
go build -o client-linux main.go

Windows主机编译MAC客户端

SET CGO_ENABLED=0
SET GOOS=darwin
SET GOARCH=amd64
go build -o client-mac main.go

Linux主机编译Widows,MAC客户端

CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o client-linux main.go
CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 go build -o client-mac main.go
CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build -o client-windows.exe main.go

MAC主机编译Widows,linux客户端

CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o client-linux main.go
CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 go build -o client-mac main.go
CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go build -o client-windows.exe main.go

golang 自动编译(live reload, auto reload, auto refresh, auto compile)比较

fresh - Build and (re)start go web apps after saving/creating/deleting source files.

Fresh is a command line tool that builds and (re)starts your web application everytime you save a Go or template file.

If the web framework you are using supports the Fresh runner, it will show build errors on your browser.

It currently works with Traffic, Martini and gocraft/web.

gin - Live reload utility for Go web servers

gin is a simple command line utility for live-reloading Go web applications. Just run gin in your app directory and your web app will be served with gin as a proxy. gin will automatically recompile your code when it detects a change. Your app will be restarted the next time it receives an HTTP request.

gin adheres to the "silence is golden" principle, so it will only complain if there was a compiler error or if you succesfully compile after an error.

realize - Go build system with file watcher, live reload and output streams. Run, build and watch file changes with custom paths

A Go build system with file watchers, output streams and live reload. Run, build and watch file changes with custom paths

Features

  • Highly customizable
  • Config your project Step by Step
  • Build, Install, Test, Fmt, Generate and Run at the same time
  • Live reload on file changes (re-build, re-install...)
  • Watch custom paths and specific file extensions
  • Watch by FsNotify or by polling
  • Support for multiple projects
  • Output streams and error logs (support for save on a file)
  • Web Panel (projects list, config settings, logs)

CompileDaemon - Very simple compile daemon for Go

Watches your .go files in a directory and invokes go build if a file changed. Nothing more.

golang的包管理工具

dep - Go dependency tool

Dep is a prototype dependency management tool. It requires Go 1.7 or newer to compile.

dep is NOT an official tool. Yet. Check out the Roadmap! Current status

Alpha. Functionality is known to be broken, missing or incomplete. Changes are planned to the CLI commands soon. It would be unwise to write scripts atop dep before then. The repository is open to solicit feedback and contributions from the community. Please see below for feedback and contribution guidelines.

Gopkg.toml and Gopkg.lock have reached a stable structure, and it is safe to commit them in your projects. We plan to add more to these files, but we guarantee these changes will be backwards-compatible.

glide - Package Management for Golang https://glide.sh

Are you used to tools such as Cargo, npm, Composer, Nuget, Pip, Maven, Bundler, or other modern package managers? If so, Glide is the comparable Go tool.

Manage your vendor and vendored packages with ease. Glide is a tool for managing the vendor directory within a Go package. This feature, first introduced in Go 1.5, allows each package to have a vendor directory containing dependent packages for the project. These vendor packages can be installed by a tool (e.g. glide), similar to go get or they can be vendored and distributed with the package.

Golang Dep

An official package manager has been started for the Go community. We view this as a good thing to bring everyone together around one solution. It's currently pre-alpha so it's not ready for prime time usage.

If you start to poke it you'll see it has a similar style to Glide. While commands may have different names there are many of the same features such as semantic versions and ranges.

Our view is that Glide will be around until an official package manager is ready for production use by the masses. We support the common tool and look forward to the day it is capable of replacing Glide.

Features

  • Ease dependency management
  • Support versioning packages including Semantic Versioning 2.0.0 support. Any constraint the github.com/Masterminds/semver package can parse can be used.
  • Support aliasing packages (e.g. for working with github forks)
  • Remove the need for munging import statements
  • Work with all of the go tools
  • Support the VCS tools that Go supports:
    • git
    • bzr
    • hg
    • svn
  • Support custom local and global plugins (see docs/plugins.md)
  • Repository caching and data caching for improved performance.
  • Flatten dependencies resolving version differences and avoiding the inclusion of a package multiple times.
  • Manage and install dependencies on-demand or vendored in your version control system.

golang queue packages

github.com/rylio/queue

Execute tasks in parallel with a concurrency limit

github.com/bgentry/que-go

An interoperable Golang port of the Ruby Que queuing library for PostgreSQL

goworker

goworker is a Go-based background worker that runs 10 to 100,000* times faster than Ruby-based workers.

goworker is compatible with Resque, so you can push your jobs with Rails and Resque, and consume them with Go in the background.

machinery

Machinery is an asynchronous task queue/job queue based on distributed message passing.

golang通过web管理worker

Writing worker queues, in Go

Have you ever wanted to write something that is highly concurrent, and performs as many tasks as you will let it, in parallel? Well, look no further, here is a guide on how to do just that, in Go!

go-workers - Sidekiq compatible background workers in golang

Sidekiq compatible background workers in golang.

  • reliable queueing for all queues using brpoplpush
  • handles retries
  • support custom middleware
  • customize concurrency per queue
  • responds to Unix signals to safely wait for jobs to finish before exiting.
  • provides stats on what jobs are currently running
  • well tested

NSQ - A realtime distributed messaging platform

NSQ is a successor to simplequeue (part of simplehttp) and as such is designed to (in no particular order):

  • support topologies that enable high-availability and eliminate SPOFs
  • address the need for stronger message delivery guarantees
  • bound the memory footprint of a single process (by persisting some messages to disk)
  • greatly simplify configuration requirements for producers and consumers
  • provide a straightforward upgrade path
  • improve efficiency

Background Jobs with Que-Go

Web servers should focus on serving users as quickly as possible. Any non-trivial work that could slow down your user’s experience should be done asynchronously outside of the web process.

Worker queues are commonly used to accomplish this goal. For a more in-depth description of the worker queue architectural pattern, read the Worker Dynos, Background Jobs and Queueing article. Here we’ll demonstrate this pattern using a sample Go application using Que-Go and Heroku Postgres.

golang hello world

安装golang

下载

wget -c https://storage.googleapis.com/golang/go1.7.1.linux-amd64.tar.gz
tar -C /usr/local -xzf go1.7.1.linux-amd64.tar.gz

vim ~/.bash_profile

export PATH=$PATH:/usr/local/go/bin

测试

go version  

设置gopath环境变量

mkdir ~/gocode
vim ~/.bash_profile

新增以下内容

export GOPATH=~/gocode
export PATH=$PATH:$GOPATH/bin

导入环境变量

source ~/.bash_profile

hello world

mkdir $GOPATH/bin 
mkdir $GOPATH/pkg 
mkdir $GOPATH/github.com/user/hello -p
cd $GOPATH/github.com/user/hello

vim hello.go

package main

import "fmt"

func main() {
        fmt.Printf("Hello, world.\n")
}

go build # 构建(可执行程序在$GOPATH/github.com/user/hello) go install # 安装(可执行程序在$GOPATH/bin) hello # 运行