GoLang及Sublime Text 2之Mac OS X 10.8.4开发环境安装

前端之家收集整理的这篇文章主要介绍了GoLang及Sublime Text 2之Mac OS X 10.8.4开发环境安装前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

2013-09-21 wcdj


1 golang简介

Go is an open source programming environment that makes it easy to build simple,reliable,and efficient software.

golang的主页:http://golang.org/


2 Mac OS X 10.8.4环境安装

(1) 下载安装包(详细可参考:http://golang.org/doc/install

http://code.google.com/p/go/downloads/list 链接中选择安装包: go1.2rc1.darwin-amd64-osx10.8.pkg

https://code.google.com/p/go/wiki/Downloads?tm=2 下载页面

(2) 安装

打开下载好的pkg文件,根据安装向导完成安装。默认的安装路径是:/usr/local/go。

  1. mba:go gerryyang$ pwd
  2. /usr/local/go
  3. mba:go gerryyang$ ls
  4. AUTHORS PATENTS api doc lib robots.txt
  5. CONTRIBUTORS README bin favicon.ico misc src
  6. LICENSE VERSION blog include pkg test
(3) 设置环境变量

将/usr/local/go/bin添加到PATH环境变量中:

a. 在home目录下打开.bashrc文件,并将添加:export PATH=$PATH:/usr/local/go/bin,然后保存关闭

b. 执行 . .bashrc 使环境变量生效;

在command line测试会显示如下内容

  1. mba:~ gerryyang$ go
  2. Go is a tool for managing Go source code.
  3.  
  4. Usage:
  5.  
  6. go command [arguments]
  7.  
  8. The commands are:
  9.  
  10. build compile packages and dependencies
  11. clean remove object files
  12. env print Go environment information
  13. fix run go tool fix on packages
  14. fmt run gofmt on package sources
  15. get download and install packages and dependencies
  16. install compile and install packages and dependencies
  17. list list packages
  18. run compile and run Go program
  19. test test packages
  20. tool run specified go tool
  21. version print Go version
  22. vet run go tool vet on packages
  23.  
  24. Use "go help [command]" for more information about a command.
  25.  
  26. Additional help topics:
  27.  
  28. c calling between Go and C/C++
  29. gopath GOPATH environment variable
  30. packages description of package lists
  31. remote remote import path Syntax
  32. testflag description of testing flags
  33. testfunc description of testing functions
  34.  
  35. Use "go help [topic]" for more information about that topic.
  36.  
  37. mba:~ gerryyang$

3 测试

下面使用MacVim编辑:

  1. package main
  2.  
  3. import "fmt"
  4.  
  5. func main() {
  6. fmt.Printf("hello,golang!\n")
  7. }


If you see the "hello,world" message then your Go installation is working.


4 配置Sublime Text 2编辑环境

(1) 下载st2 http://www.sublimetext.com/

(2) 安装gosublime插件 http://my.oschina.net/Obahua/blog/110767

(3) st2个性化配置及入门使用(比如,更改图标)

英文原文 http://net.tutsplus.com/tutorials/tools-and-tips/sublime-text-2-tips-and-tricks/

中文 http://lucifr.com/2011/08/31/sublime-text-2-tricks-and-tips/


st2的一些优点:

(a) 快捷的命令面板 shift+command+p

(b) 即时的文件切换 command+p

(c) 随心所欲的跳转 command+p @ (函数) #(搜索) : (行数)

PS: 让人叫绝的是,这些切换定位方法可以配合在一起使用

(d) 多重选择 (这个功能确实爽,对于不熟悉正则匹配的人)

激活多重选择的方法有几种:

  • 按住 CommandAlt,然后在页面中希望中现光标的位置点击。
  • 选择数行文本,然后按下 Shift + Command + L
  • 通过反复按下 Control/Command + D 即可将全文中与光标当前所在位置的词相同的词逐一加入选择,而直接按下 Alt+F3(Windows) 或是 Ctrl+Command+G(Mac) 即可一次性选择所有相同的词。
  • 按下鼠标中键(Mac上是option键)来进行垂直方向的纵列选择,也可以进入多重编辑状态。

(e) 代码缩进显示

缩进指示已经被集成进 Sublime Text 2 中,不需要安装插件了,之前需要安装 IndentGuides 插件

(f) 安装包控制

安装 Package Control 的方法

https://packagecontrol.io/installation

  • 打开 Sublime Text 2,按下 Control + ` 调出 Console
  • 将以下代码粘贴进命令行中并回车:

    1. 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 请使用以下代码

    1. 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,'wb').write(urllib.request.urlopen( 'http://sublime.wbond.net/' + pf.replace(' ','%20')).read())

  • 重启 Sublime Text 2,如果在 Preferences -> Package Settings中见到Package Control这一项,就说明安装成功了。

使用 Package Control 安装 Package 的方法

第一步:按下 Shift + Command + P 调出命令面板

第二步:输入 install 调出 Package Control: Install Package 选项,按下回车

第三步:在列表中找到 xxx插件,按下回车进行安装

第四步:重启 Sublime Text 2 使之生效

PS: 详细的package列表可以在 https://sublime.wbond.net/ 查询


(g) 对齐

在 Sublime Text 2 之中,一个 Sublime Alignment 插件也可以轻松实现。


(h) Vim模式 (方便Vimer过度使用)

Sublime Text 2 dev 版已经支持 Vim 的编辑模式了,如果更喜欢 Vim 的编辑模式,可以通过以下方法来激活 Vintage mode。

第一步:按下 Shift + Command + P 调出命令面板

第二步:输入 settings user 调出 Preferences:Settings - User,并按下回车

(以上两步等价于:command +,)

第三步:这时会打开一个 Preferences.sublime-settings 的文件,如果是第一次修改,它应该是个空文件,把以下文本粘贴进去

  1. {
  2. "ignored_packages": []
  3. }

第四步:保存这个文件,这时按下 ESC 键,再按下一些你熟悉的 Vim命令。

说明:gd命令好像不能用,跳转查询等是相同的,Vintage 模式本身就是通过 keybinding 实现的,可以参考修改 http://sublimetext.info/docs/en/reference/key_bindings.html


(i) 无干扰模式(Distraction Free Mode)(自己还是习惯全屏模式 control + command + f)

在 Sublime Text 2 中,只要按下 Control + Shift + Command + F 或是在菜单 View 中选择 Enter Distraction Free Mode 就可以进入这个 UI 最小化模式了。如果是在用 Mac OS X Lion 的话,Sublime Text 2 还同时支持 Lion 的原生全屏模式。

通过修改 “Preferences” -> “File Settings - More” -> “Distraction Free - User” 可以对防干扰模式进行一些设置:

  1. {
  2. "line_numbers": false,//是否显示行号
  3. "gutter": false,//是否显示边列
  4. "draw_centered": true,//是否居中显示
  5. "wrap_width": 80,//换行宽度(单位:字符)
  6. "word_wrap": true,//是否自动换行
  7. "scroll_past_end": true //滚动能否超过结尾
  8. }

(j) 一些编辑显示技巧

(1) 在左侧栏显示打开的文件列表

command + k; command + b 实现打开和关闭之间的切换。

(2) 字体大小动态调整

command + = 调大

command + - 调小

(3) cmd + option + 数字

实现多个tab并行编辑和对比。

(4) 通过参考默认配置文件 Perferences.sublime-settings 中的参数,可进行个性化设置。


(k) 主题

这里所讲的主题不同于针对代码的 Color Scheme,是指针对 Sublime 程序本身的主题,目前可以安装的是 Ian Hill 的 Soda


(l) 必备的扩展插件

(a) ConvertToUTF8

ST2只支持utf8编码,该插件可以显示与编辑 GBK,BIG5,EUC-KR,EUC-JP,Shift_JIS 等编码的文件

https://sublime.wbond.net/packages/ConvertToUTF8

https://github.com/seanliang/ConvertToUTF8

(b) gosublime

GoSublime is a Golang plugin collection for the text editor SublimeText providing code completion and other IDE-like features. Both Sublime Text versions 2 and 3 are supported.

https://sublime.wbond.net/packages/GoSublime



(m) 文档

Sublime Text Unofficial Documentation

http://www.sublimetext.com/docs/2/


(n) 把subl添加命令行中

在 .bashrc 配置中添加如下别名:

  1. # Sublime Text 2
  2. alias subl='/Applications/Sublime\ Text\ 2.app/Contents/SharedSupport/bin/subl'

然后 . .bashrc 使配置生效。然后输入 subl -h则显示如下:
  1. mba:bin gerryyang$ subl -h
  2. Sublime Text 2 Build 2221
  3.  
  4. Usage: subl [arguments] [files] edit the given files
  5. or: subl [arguments] [directories] open the given directories
  6. or: subl [arguments] - edit stdin
  7.  
  8. Arguments:
  9. --project <project>: Load the given project
  10. --command <command>: Run the given command
  11. -n or --new-window: Open a new window
  12. -a or --add: Add folders to the current window
  13. -w or --wait: Wait for the files to be closed before returning
  14. -b or --background: Don't activate the application
  15. -s or --stay: Keep the application activated after closing the file
  16. -h or --help: Show help (this message) and exit
  17. -v or --version: Show version and exit
  18.  
  19. --wait is implied if reading from stdin. Use --stay to not switch back
  20. to the terminal when a file is closed (only relevant if waiting for a file).
  21.  
  22. Filenames may be given a :line or :line:column suffix to open at a specific
  23. location.
  24.  
  25. mba:bin gerryyang$ subl -v
  26. Sublime Text 2 Build 2221
  27.  
  28. mba:bin gerryyang$

在st2编写golang的模样:



5 进一步的使用

Start by taking A Tour of Go.

Build a web application by following the WikiTutorial.

Read Effective Go to learn about writingidiomatic Go code.

For the full story,consult Go's extensive documentation.

Subscribe to thegolang-announcemailing list to be notified when a new stable version of Go is released.


6 参考

[1] http://golang.org/

[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/ Sublime Text 2 入门及技巧

[5] http://lucifr.com/tags/sublime-text/ 更多st2的文章

猜你在找的Go相关文章