GoLang及Sublime Text 2之Mac OS X 10.8.4开发环境安装
2013-09-21 wcdj
1 golang简介
Go serves as an open-source programming platform that enables one to easily construct simple, reliable, and efficient software constructions.
golang的主页:http://golang.org/
2 Mac OS X 10.8.4环境安装
(1) 下载安装包(详细可参考:http://golang.org/doc/install)
前往该页面并下载所需文件:go1.2rc1.darwin-amd64-osx10.8.pkg
https://code.google.com/p/go/wiki/Downloads?tm=2 下载页面
(2) 安装
启动已打包好的包文件,并按照指导完成安装流程。默认情况下,默认路径设置为$ /usr/local/go。
mba:go gerryyang$ pwd
/usr/local/go
mba:go gerryyang$ ls
AUTHORS PATENTS api doc lib robots.txt
CONTRIBUTORS README bin favicon.ico misc src
LICENSE VERSION blog include pkg test
(3) 设置环境变量
将/usr/local/go/bin添加到PATH环境变量中:
请以当前目录为home,在.bashrc文件中进行如下设置:export PATH=$PATH:/usr/local/go/bin,并保存退出。
b. 执行 . .bashrc 使环境变量生效;
在command line测试会显示如下内容:
mba:~ gerryyang$ go
Go is a tool for managing Go source code.
Usage:
go command [arguments]
The commands are:
build compile packages and dependencies
clean remove object files
env print Go environment information
fix run go tool fix on packages
fmt run gofmt on package sources
get download and install packages and dependencies
install compile and install packages and dependencies
list list packages
run compile and run Go program
test test packages
tool run specified go tool
version print Go version
vet run go tool vet on packages
Use "go help [command]" for more information about a command.
Additional help topics:
c calling between Go and C/C++
gopath GOPATH environment variable
packages description of package lists
remote remote import path syntax
testflag description of testing flags
testfunc description of testing functions
Use "go help [topic]" for more information about that topic.
mba:~ gerryyang$
3 测试
下面使用MacVim编辑:
package main
import "fmt"
func main() {
fmt.Printf("hello, golang!\n")
}

If you see the "hello, world" message then your Go installation is working.
4 配置Sublime Text 2编辑环境
(1) 下载st2 http://www.sublimetext.com/
(2) 请安装gomsublime插件,并点击上方提供的链接前往下载或安装说明
(3) st2个性化配置及入门使用(比如,更改图标)
The site offers a variety of practical tips and tricks for enhancing your productivity with Sublime Text 2. It provides a wealth of information aimed at optimizing user experience, including detailed instructions on how to customize the interface, manage packages efficiently, and integrate with other tools seamlessly. The tutorials are designed to cater to both novice and advanced users, ensuring that everyone can make the most of this powerful text editor. By following these tips, users can significantly improve their workflow and achieve better results in their daily tasks.
这个资源提供了Sublime Text 2的一些技巧和快捷键。
你可以在这里找到很多实用的小技巧。
这个页面详细介绍了Sublime Text 2的各种功能。
如果你对Sublime Text 2感兴趣,
这个地方是一个很好的学习资源。
点击这里可以访问到原始的文章。
st2的一些优点:
(a) 快捷的命令面板 shift+command+p
(b) 即时的文件切换 command+p
(c) 随心所欲的跳转 command+p @ (函数) #(搜索) : (行数)
PS: 让人叫绝的是,这些切换定位方法可以配合在一起使用 。
(d) 多重选择 (这个功能确实爽,对于不熟悉正则匹配的人)
激活多重选择的方法有几种:
- 使用命令键组合(包括Command和Alt)进行操作
- 选定多行文字内容后执行快捷方式:Shift+Command+L
- 持续执行快捷方式:Control/Command + D 可以将所有相同词逐一选中
- 同样地,在Windows系统上使用快捷方式:Alt+F3,在Mac系统上则为Ctrl+Command+G 可完成批量选词
- 使用快捷方式:Option键进行垂直方向的选择
(e) 代码缩进显示
缩进规则已整合到Sublime Text 2中不再需额外配置插件此前须安装 IndentGuides 插件
(f) 安装包控制
安装 Package Control 的方法:
https://packagecontrol.io/installation
- 打开 Sublime Text 2,按下 `Control + `` 调出 Console
将以下代码粘贴进命令行中并回车:
import urllib2,os; pf='Package Control.sublime-package'; ipp = sublime.installed_packages_path(); os.makedirs( ipp ) if not os.path.exists(ipp) else None; urllib2.install_opener( urllib2.build_opener( urllib2.ProxyHandler( ))); open( os.path.join( ipp, pf), 'wb' ).write( urllib2.urlopen( 'http://sublime.wbond.net/' +pf.replace( ' ','%20' )).read()); print( 'Please restart Sublime Text to finish installation')
Sublime Text 3 请使用以下代码:
import urllib.request,os; pf = 'Package Control.sublime-package'; ipp = sublime.installed_packages_path(); urllib.request.install_opener( urllib.request.build_opener( urllib.request.ProxyHandler()) ); open(os.path.join(ipp, pf), 'wb').write(urllib.request.urlopen( 'http://sublime.wbond.net/' + pf.replace(' ','%20')).read())
请重新启动Sublime Text 2程序。在软件偏好设置选项卡下的包管理器设置中,请查看单独的一项Package Control。如果发现该设置项,则表示安装已成功完成。
使用 Package Control 安装 Package 的方法:
第一步:按下 Shift + Command + P 调出命令面板
第二步:在终端中输入 install 命令后,在界面上打开 Package Control: Install Package 选项框,并按回车键执行安装操作。
第三步:在列表中找到 xxx插件,按下回车进行安装
第四步:重启 Sublime Text 2 使之生效
注解:核心术语可以在 https://sublime.wbond.net/ 获取信息。
(g) 对齐
在Sub lime Text 2平台上,在Sub lime Alignment工具包中也能够方便地实现。
(h) Vim模式 (方便Vimer过度使用)
Sublime Text 2 发行版已内置 Vim 的编辑模式功能。若您偏好采用 Vim 的编辑模式,则可通过特定配置选项设置启用 Vintage 模式。
第一步:按下 Shift + Command + P 调出命令面板
第二步:输入 settings user 调出 Preferences:Settings - User,并按下回车
(以上两步等价于:command + , )
第三步:这时会打开一个 Preferences.sublime-settings 的文件,如果是第一次修改,它应该是个空文件,把以下文本粘贴进去
{
"ignored_packages": []
}
第四步:保存这个文件,这时按下 ESC 键,再按下一些你熟悉的 Vim命令。
说明:gd命令似乎也不太好用。跳转、查询等操作也都是相同的。Vintage模式本身是基于key绑定实现的。可以参考修改: http://sublimetext.info/docs/en/reference/key_bindings.html
(i) 无干扰模式设置(Distraction Free Mode)(通常偏好全屏显示方式 control + command + f)
在 Sublime Text 2 中,你可以通过点击 Control + Shift + Command + F 或者在菜单栏中选择 Enter Distraction Free Mode 来开启这个 UI 的最小化模式。如果使用的是 Mac OS X Lion 版本的话,默认情况下 Sublime Text 2 还会支持该操作系统的内置全屏模式功能。
通过调整"Preference Settings" → "File Settings > Advanced Options" → "Distraction-Free Mode: User Interface" 可以对防干扰模式进行一些设置:```
{
"line_numbers": false, //是否显示行号
"gutter": false, //是否显示边列
"draw_centered": true, //是否居中显示
"wrap_width": 80, //换行宽度(单位:字符)
"word_wrap": true, //是否自动换行
"scroll_past_end": true //滚动能否超过结尾
}
``
(j) 一些编辑显示技巧
(1) 在左侧栏显示打开的文件列表
command + k; command + b 实现打开和关闭之间的切换。
(2) 字体大小动态调整
command + = 调大
command + - 调小
(3) cmd + option + 数字
实现多个tab并行编辑和对比。
依据 Sublime Preferences 预设配置文件中的相关参数信息, 即可实现特定化配置.
(k) 主题
此处所讨论的主题与基于代码的颜色方案不同。它即指适用于Sublime程序本身的主题。目前可以安装的是 Ian Hill 提供的 Soda 主题包(Soda)。
(l) 必备的扩展插件
(a) ConvertToUTF8
ST2仅限于 utf8 编码的支持范围之内,在此插件下可实现显示并编辑包括 EKB、BIG5、EUC-KR、EUC-JP 以及 Shift_JIS 等编码文件的操作功能
https://sublime.wbond.net/packages/ConvertToUTF8
https://github.com/seanliang/ConvertToUTF8
(b) gosublime
GoSublime consists of a collection of Golang plugins designed for the SublimeText text editor (http://www.sublimetext.com), offering features such as code completion and an IDE-like interface. The plugin supports both Sublime Text version 2 and version 3.
https://sublime.wbond.net/packages/GoSublime
(m) 文档
http://www.sublimetext.com/docs/2/
(n) 把subl添加在命令行中
在 .bashrc 配置中添加如下别名:
# Sublime Text 2
alias subl='/Applications/Sublime\ Text\ 2.app/Contents/SharedSupport/bin/subl'
然后 . .bashrc 使配置生效。然后输入 subl -h则显示如下:
mba:bin gerryyang$ subl -h
Sublime Text 2 Build 2221
Usage: subl [arguments] [files] edit the given files
or: subl [arguments] [directories] open the given directories
or: subl [arguments] - edit stdin
Arguments:
--project <project>: Load the given project
--command <command>: Run the given command
-n or --new-window: Open a new window
-a or --add: Add folders to the current window
-w or --wait: Wait for the files to be closed before returning
-b or --background: Don't activate the application
-s or --stay: Keep the application activated after closing the file
-h or --help: Show help (this message) and exit
-v or --version: Show version and exit
--wait is implied if reading from stdin. Use --stay to not switch back
to the terminal when a file is closed (only relevant if waiting for a file).
Filenames may be given a :line or :line:column suffix to open at a specific
location.
mba:bin gerryyang$ subl -v
Sublime Text 2 Build 2221
mba:bin gerryyang$
在st2编写golang的模样:

5 进一步的使用
Start by taking A Tour of Go.
Construct a Web application through following Wiki Tutorial.
View Effective Go to explore idiomatic Go code.
To learn more about the full story, refer to Go's detailed documentation.
Join the Go Developers' golang-announce newsletter to stay informed about updates whenever a new stable version of Go is released.
6 参考
[2] https://github.com/Unknwon/go-fundamental-programming go编程基础
[3] http://www.sublimetext.com/ st2主页
第4篇 http://lucifr.com/2011/08/31/sublime-text-2-tricks-and-tips/
其他相关的st2文章
