Programming in Go (Golang) – Setting up a Mac OS X Development Environment

http://www.distilnetworks.com/setup-go-golang-ide-for-mac-os-x/#.V1Byrf50yM8

Programming in Go (Golang) – Setting up a Mac OS X Development Environment

Published on February 4, 2015

At Distil, we have recently started to use Go (Golang) to expand the functionality of our data platform.  A surprisingly challenging aspect of getting started with Go was setting up a development environment on my MacBook.  We learned a few things along the way and hopefully information below will help you as you setup your Go development environment.

Project Workspace

The GOPATH environment variable is a key component to building Go applications.  There are several approaches to manage your project workspace and external package dependencies using the $GOPATH.  We decided to have one GOPATH based on the conventions in “How To Write Go Code”.  For example, here are my .profile settings:

### Go
export GOROOT=/usr/local/Cellar/go/1.3.3/libexec
export PATH=$PATH:$GOROOT
GOPATH=$HOME/distil/projects/go
export PATH=$PATH:$GOPATH/bin

We use GitHub and each Go project has a separate repository.  If the Go project requires multiple application binaries, we organize those under a separate folders.  This was inspired by Brad Fitzpatrick’s Camlistore project.  Below is an example of the Go directory layout based on the GOPATH above:

~/distil/projects/go/src/github.com/distil/
  project1/
    cmd/
      app1/
        main.go  
      app2/
        main.go
    models/
      customer/
        customer.go
    pkg/
      aws/
        s3.go
        ec2.go
      db/
        postgres/
          postgres.go
  project2/

Dependency Management

A variety of package managers have been built to help with Go dependency management.  I would encourage you experiment to see what works best for you and your organization, but we wanted to keep things simple.  We use the gpmtool which allows you to version external packages through the use of a Godeps file.  Each of our projects have their own Godeps file and the dependencies are installed locally based on the single GOPATH described above.  The only caveat is that running `gpm install` for different projects with conflicting versions of the same package in the Godeps file will overwrite that package locally.  This is manageable as long as you update the dependencies for a project when you switch between projects.

Golang Mac IDE vs Editor

Using an IDE for Go is entirely optional and a personal preference.  All you really need is a text editor and a terminal.  The trick is finding the right tool(s) which allow you to be productive and efficient.  If you are accustomed to using tools like RubyMine, IntelliJ or Eclipse, you will likely find it disappointing there are no equivalent, full-featured IDEs for Go.  So what’s the best IDE for Golang? After trying several options which included LiteIDE, IntelliJ Go Plugin, Go-IDE and Atom, I personally settled on using Sublime Text and iTerm.

Sublime Text

Sublime Text 3 along with the GoSublime package provides several features to improve Go programming productivity.  I also setup a Sublime Project for each of my Go projects which contains a sublime-project file and sublime-workspace file.  If your project needs specific environment variables you can set those in your sublime-project file by adding the following:

{
  "settings": {
    "GoSublime": {
      "env": {
        "APP1_ENV": "test",
        "APP1_HOME": "/var/app1"
      }
    }
  }
}

When getting started with Sublime, I stubbled upon the Get Go-ing with Sublime post by Tyler Bunnell which contains several tips and tricks when using Sublime and GoSublime.  If you decide to use Sublime, I encourage you to check out Tyler’s post.  The best tip is customizing the preferences to run `go build` and `go test` upon saving a go file which I slightly modified.  Below are the Gosublime preferences I use:

{
  "on_save": [
    {
      "cmd": "gs9o_open",
      "args": {
        "run": [
          "sh",
          "go build && go test -i && go test && go fmt && go vet"
        ],
        "focus_view": false
      }  
    }
  ],
  "autocomplete_closures": true,
  "complete_builtins": true,
  "fmt_cmd": [
    "goimports"
  ]
}

In addition to using the GoSublime package, I also use the SublimeGit and GitGutter packages to interact with GitHub.  When I am ready to run my application, I use iTerm2 instead of trying to run in the GoSublime console.  Sublime Text along with the numerous packages provide a ton of shortcuts and features to improve productivity.

Setting up my current Go development environment did not happen overnight.  In fact, I spent numerous hours researching and evaluating different tools, best practices and conventions.  I encourage you to find a setup that works best for you and hopefully this information will save you a couple of hours!

时间: 2024-10-14 09:03:50

Programming in Go (Golang) – Setting up a Mac OS X Development Environment的相关文章

Setting up a Mac OS build environment

In a default installation, Mac OS runs on a case-preserving but case-insensitive filesystem. This type of filesystem is not supported by git and will cause some git commands (such as git status) to behave abnormally. Because of this, we recommend tha

Mac OS 安装golang

下载golang安装包 下载地址: http://www.golangtc.com/download https://code.google.com/p/go/downloads/list go1.4.darwin-amd64-osx10.8.pkg go1.4 Mac OS X (x86 64-bit) PKG installer 设置环境变量 配置 GOROOT 和 GOPATH: 创建目录下的go文件夹: mkdir ~/go 下面的东西放到.bash_rc(也可能是.bash_profi

COCOA® PROGRAMMING FOR MAC® OS X (1)- Get Start

这个是Mac OS App开发学习的第一篇,希望是一个好的开始. 一.为什么要学习Mac OS App开发 a)长时间做IOS开发,发现自己所做的局限在苹果的一些库上面,一些底层的API却是私有的,不利于自己对操作系统的理解,学习Mac OS 可以增加对底层系统的理解,OC也有更广泛的应用空间. b) 平时使用Mac 发现上面的一些软件并不好用,如果自己写的话,相信可以写出更好的. c) Mac OS开发和其它所有的开发工作都一样,除了编码层次上面,更希望提高自己宏观的软件设计能力.扩展眼界,提

Mac OS X 安装Golang

1.确保Mac OS X 安装了Git,Hg,在Mac OS X 10.8+下面安装Xcode CommandLine Tools就可以了,10.9以上安装Xcode就包含了Git工具,hg请安装SourceTree,这个Git GUI工具包含了Mercurial工具 2.下载安装go1.3.1.darwin-amd64-osx10.8.pkg 3.安装Sublime Text,一次安装插件,GoSublime,配置GoSublime-Settings-User文件,切记不要改动任何Defaul

[转]“WARNING: soft rlimits too low” in MongoDB with Mac OS X

转自:Programming and Technology If you get this warning when you connect to mongo shell in Mac OX X: ** WARNING: soft rlimits too low. Number of files is 256, should be at least 1000 A simple way to fix this is setting the limit just before starting mo

使用CocoaPods的pod install 遇到的错误,未解决(Xcode版本v6.4,Mac OS版本v10.10.4)

转载请标明出处:http://blog.csdn.net/android_ls/article/details/47059957 一.CocoaPods 是一款很好的依赖管理工具,其安装步骤大致如下: 1.首先确保开发环境中已经安装了 Ruby(一般安装了 XCode,Ruby 会被自动安装上) 建议使用淘宝提供的 Gem源,在终端执行下列命令: $ gem sources --remove https://rubygems.org/ $ gem sources -a http://ruby.t

MAC OS X socket 1 [a simple example]

Server Code C: // // main.c // unix_socket_very_simple_server // // Created by DMD on 4/7/14. /* Function : Server for unix (OS X MAC) */ #include <stdio.h> #include <stdlib.h> #include <sys/types.h> // unix socket #include <sys/socke

Windows下虚拟机安装Mac OS X &mdash;&ndash; VM12安装Mac OS X 10.11

______________________________________________________________________________________________________________________________________________________________________________________________________________________ 注:本文来源:csdn:N的专栏.<Windows下虚拟机安装Mac

Mac OS X 背后的故事

Mac OS X 背后的故事 作者: 王越  来源: <程序员>  发布时间: 2013-01-22 10:55  阅读: 25840 次  推荐: 49   原文链接   [收藏] 作者王越,美国宾夕法尼亚大学计算机系研究生,中国著名 TeX 开发者,非著名 OpenFOAM 开发者. Mac OS X 背后的故事(一)力挽狂澜的Ellen Hancock Mac OS X 背后的故事(二)Linus Torvalds的短视 Mac OS X 背后的故事(三)Mach之父Avie Tevan