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

来源:http://www.cnblogs.com/kungfupanda/archive/2016/06/03/5554962.html
-Advertisement-
Play Games

http://www.distilnetworks.com/setup-go-golang-ide-for-mac-os-x/#.V1Byrf50yM8 Programming in Go (Golang) – Setting up a Mac OS X Development Environmen ...


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

Golang GopherAt 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 gpm tool 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!


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 動機 今天有朋友寫信說他認為自己的wordpress博客內顯示的訪問統計信息不正常,希望我能為他製造一些訪問信息,供他對比。朋友提出的請求是在短時間內快速打開100個不同的博客頁面,以便他從產生的訪問量變化中理解博客訪問數據。 本人作為一個搞電腦的人,有把任何重覆性勞動自動化的衝動,所以雖然點開1 ...
  • 本文示例代碼測試環境是Windows下的APMServ(PHP5.2.6) 簡述 可能大家都知道,php中有一個函數叫debug_backtrace,它可以回溯跟蹤函數的調用信息,可以說是一個調試利器。 好,來複習一下。 one(); function one() { two(); } functi ...
  • C++這門語言從誕生到今天已經經歷了將近30個年頭。不可否認,它的學習難度都比其它語言較高。而它的學習難度,主要來自於它的複雜性。現在C++的使用範圍比以前已經少了很多,java、C#、python等語言在很多方面已經可以代替C++。但是也有很多地方是其他語言完全無法替代的,主要集中在需要運行效率比 ...
  • Ok,我又來寫SOLR的內容了,昨天做了修改和增加的增量索引,今天來說說關於刪除的增量索引以及定時更新增量索引,廢話不多說,下麵進入正文。 一、增量索引之刪除 昨天已經說過,增量索引其實就是SOLR對上次做過(增量或者全量)索引之後到這次做索引之間的這段時間資料庫發生變化的數據進行創建索引,昨天我們 ...
  • 1. D:\AWS_workspace\DAAS_Go>go get -u -ldflags -H=windowsgui github.com/nsf/gocode 2. Then gocode.exe will be installed in "D:\AWS_workspace\DAAS_Go\b ...
  • http://blog.csdn.net/hengyunabc/article/details/7371446 本文章地址:http://blog.csdn.net/hengyunabc/article/details/7371446 2012-03-31:新版的goclipse改進很大,本文有些內 ...
  • 最近想學習下Go語言,先從最基本的Hello Go開始,搭建Go開發環境 一、下載Go包 由於Go官網國內訪問經常有問題,可以從國內的鏡像下載: http://www.golangtc.com/ 二、配置環境變數 1、將Go的壓縮包解壓到任意目錄,我的為D:\SDE\go 2、 配置GoRoot路徑 ...
  • http://www.cnblogs.com/qgymje/p/3912259.html 這篇原來是給公司里使用go開發的交流用的,主要是工具的安裝,用markdown寫的,發佈了內部gitlab上,倉促發佈到blog上面,也沒改啥格式,現在整理一下,然後增加一點說明 基礎開發工具 sublime ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...