Sublime for Mac Development Golang: Step-by-step environment configuration

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Installing Golang

Download the installation package directly on the official website. Download the most recent installation package in PKG format, double click to run, follow the instructions to complete the installation.

Or use brew for installationbrew install go

After the installation is complete, open terminal, enter go version , and check that the Golang SDK is installed successfully.

➜~ Go versiongo version go1. 3. 3 darwin/amd64

Environment variable Configuration

Gopath is used to tell the Golang command and other related tools where to find your go package directory.

Gopath is a list of paths, similar to the configuration of path.

Gopath=~/workspace/demo/go-demo

I wrote the Golang configuration in a separate. golangrc file and introduced it in. ZSHRC

SOURCE ~/.GOLANGRC

If you configure multiple directories in the Golang, when you download the Open source package is ( go get ***** ), the open source package by default will find the first directory, will be unified down to the first directory of the PKG directory.

Go official recommended project structure

Bin/hello # command executableoutyet # command executablepkg/linux_amd64/github.com/golang/example /STRINGUTIL.A # Package Objectsrc/github.com/golang/example/.git/ # git repository metadatahello/ Hello.go # command sourceoutyet/main.go # command sourcemain_test.go # test SourceStringutil /reverse.go # package sourcereverse_test.go # test Source

Reference

Http://golang.org/doc/code.html

Sublime Installation Gosublime

How to install the package control I will not say. Installing the Gosublime plug-in directly

Mac OSX Use COMMAND + Shift + p to open Package Control , and then enter pcip (the package control:install the abbreviation)

Enter in the subsequent interface GoSublime , carriage return, start to install Gosubmlime.

When you can see in sublime Preferences -> Package Settings GoSublime , then congratulations, your sublime-based Golang development environment is built.

Here I choose is sublime text installation Gosublime plug-in development (Golang syntax highlighting hints), the installation method here is not much mention.

After you have configured your development environment, simply do the go language development. Under the SRC directory under your gopath, create a new folder (named after the project name), and then open the folder in Sublime, and the new Main.go file can be encoded.

Packagemainimport ("FMT") Funcmain () {fmt. Println ("Hello");}

After the code is written, use Command+b to open the sublime text terminal, compile it with Go build xx (project name), and compile the resulting information as follows:

[' Go build Main.go ' | done:320.650295ms]

After compiling successfully, execute the shell command and execute the file just after compiling./hellogo to see the results of the operation:

['./main ' | done:12.281054ms] Hello

If you simply want to see the results of the run without producing an executable file (like the file name and project name), you can use go run xxx.go directly in the sublime text terminal:

[' Go run Main.go ' | done:222.787159ms] Hello

So far, we have installed the basic environment for developing GOLANG programs!

Simply review the process:

Go language is Google launched by a programming language, is considered to be the C language of 21st century, this article describes how to Mac OS set up the Go language development environment, the quickest way to use the brew installation go, and then set the corresponding environment variables.

Install Go

$ brew install go

Setting environment variables

I use the other, and so on Shell zsh , to edit the home directory .zprofile , if used, the bash corresponding file is~/.bash_profile

12345 $ vim. Zprofile# Go Developmentexport gopath=$HOME/dev/goexport goroot="$ ( Brew--prefix Golang)/libexec "export path="$PATH:$GOPATH/bin:$GOROOT/bin "

Make environment variables effective

Executing at the command line terminal

$ sourse .zprofile

See if environment variables are in effect

123456789101112131415161718192021 $ Go envgoarch= "AMD64" gobin= "/user/pengshp/dev /go/bin " goexe= gohostarch= gohostos=< Span class= "string" > "Darwin" goos= "Darwin" gopath= "/users /pengshp/dev/go " gorace= goroot="/usr/local/opt/go/ Libexec " gotooldir= gccgo=< Span class= "string" > "Gccgo" cc= cxx= "clang++" cgo_enabled= "1" cgo_cflags= "-g-o2" cgo_cppflags=< Span class= "string" "" cgo_cxxflags= "-g-o2" cgo_fflags=- G-o2 " cgo_ldflags= pkg_config= "Pkg-config"

Development tools

For the initial learning old said, development tools can choose Sublime Text 3 or is VScode , sublime install a GoSublime plug-in, Vscode also has the corresponding plug-in.




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.