Configuring Go Development Environment in Sublime Text 3
Installing Git
Since Go utilizes Git for managing external packages, the initial step involves installing Git. Visit http://www.git-scm.com/download/ to download the appropriate version for your operating system. The installation process is straightforward—simply proceed through the installer. Ensure that "Git Bash here" and "Git GUI here" are selected under Windows Explorer integration.
Installing Go
-
Navigate to https://golang.org/dl/ to select a suitable package for your platform (for users behind a firewall, use http://golangtc.com/download).
-
Download the file
go1.4.2.windows-amd64.msiand install it to a designated directory, such asE:\Go. -
After installation, verify the setup by running
go envin the command prompt:
$ go env
set GOARCH=amd64
set GOBIN=E:\GoWorkSpace\bin
set GOCACHE=C:\Users\Administrator\AppData\Local\go-build
set GOENV=C:\Users\Administrator\AppData\Roaming\go\env
set GOEXE=.exe
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOINSECURE=
set GONOPROXY=
set GONOSUMDB=
set GOOS=windows
set GOPATH=E:\GoWorkSpace\
set GOROOT=E:\Editor\Go
Confirm that the go command functions correctly. If not, review environment variable configurations.
Setting Up Go Environment Variables
Access the system properties through "My Computer" -> "Advanced Settings" -> "Enviroment Variables".
-
Check if the
GOROOTvariable exists and ensurePathincludes%GOROOT%\bin.If missing, create a new
GOROOTvariable pointing to your Go root directory (e.g.,E:\Editor\Go). Then updatePathto append;%GOROOT%\bin. -
Configure
GOPATH, which defines your project workspace. This is distinct from the Go installation path. Multiple directories can be specified using semicolons on Windows or colons on Linux. The first directory will be used by default when executinggo get. Create a new variable namedGOPATHset toE:\GoWorkSpace. -
GOBINdefaults to thebinsubdirectory withinGOPATH. For example:E:\GoWorkSpace\bin
Installing Sublime Text
-
Download Sublime Text from http://www.sublimetext.com/
-
Install Package Control by opening the console via View -> Show Console and pasting the following script:
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())
Press Enter and restart Sublime Text once complete.
To manually install the GoSublime plugin:
-
Open Preferences -> Browse Packages to locate the packages directory, e.g.,
C:\Users\...\AppData\Roaming\Sublime Text 3\Packages -
If Git is available, navigate into the folder and execute:
git clone https://github.com/DisposaBoy/GoSublime.git
Alternatively, download the zip archive from https://github.com/DisposaBoy/GoSublime and extract it into the packages directory. Rename the extracted folder to GoSublime.
- Restart Sublime Text to finalize installation.
Configuring Auto-Execution with Ctrl+B
To enable automatic execution of Go programs using Ctrl+B:
-
In Preferences -> Browse Packages, locate the
GoSublime/srcdirectory. Create a newmargosubfolder. -
Copy
GoSublime/src/margo.sh/extension-example/extension/extension-example.gointo the newly createdmargofolder and rename it tomargo.go. -
Define a custom build system by going to Tools -> Build System -> New Build System. Replace the default content with:
{
"encoding":"utf-8",
"cmd": ["E:/Editor/Go/bin/go","run","$file"],
"file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)"
}
Save this configuration as GoBuild.sublime-build. Select it via Tools -> Build System -> GoBuild to activate.
Upon successful configuration, pressing Ctrl+B should compile and execute the current Go file.