Mac OS x under Go install, use, delete

Source: Internet
Author: User

Download and install
download
-Download and install the installation package on the official website
installation
All the way to install by default
Then run go version in the terminal. If a message similar to the following is displayed, the installation is successful
go version go1.10.1 darwin / amd64
If the above information does not appear or other error messages appear, please inquire and solve it by yourself
GOROOT and GOPATH and bin settings
Create a workspace, such as $ HOME / go. (If you want to create a workspace in a different directory, then you need to set the GOPATH environment variable and bin environment variable)

Set GOPATH environment variable

Edit your ~ / .bash_profile (run vi ~ / .bash_profile in the terminal) to add the following line of code (if you can't find .bash_profile, you can create one yourself)
export GOPATH = $ HOME / go
Save and exit your editor. Then run the following command in the terminal
source ~ / .bash_profile
In the official Go documentation, this is how to set the path of go's binary file directory

Note, set the GOBIN path to produce binary files when running go install
export GOBIN = $ HOME / go / bin
In other online documentation, set the path directly
export PATH = $ HOME / go / bin
This is what my bash_profile file looks like

export GOPATH = $ HOME / go
export PATH = $ HOME / go / bin: $ PATH
If you install directly through the installation package under mac os x, you do n’t need to set bin, because when installing through the installation package (unless your work space is not in $ HOME / go but in other file directories you prefer, this When you need to set the bin directory), the bin directory has been specified under /etc/paths.d/go
$ cd /etc/paths.d
$ ls
go
$ cat go
/ usr / local / go / bin

GOROOT defaults to / usr / local / go, if not, it can be set in the bash_profile file

export GOROOT = / usr / local / go
Then exit the editor, and then run the source ~ / .bash_profile command

The first go program
Enter into the workspace (mine is $ HOME / go, so use the cd $ HOME / go command to enter directly)
Then create a directory src / hello (mkdir src / hello),
Then enter into the directory (cd src / hello),
Then use vi hello.go or other ways to create a hello.go file,
Then open the file for editing
package main

import "fmt"

func main () {
    fmt.Printf ("hello, world \ n")
}
Save and exit
Then run go build in the hello.go file directory, this command will create an executable file named hello,
If you execute the command ls, you will see that the executable file is next to hello.go
$ go build hello.go
$ ls
hello hello.go
Then execute the command ./hello
$ ./hello
hello, world!
If you see hello, world, the program is running correctly, otherwise it is wrong

You can run go install hello to install the binary file into the bin directory of the workspace ($ HOME / go), or go clean hello to delete the binary file
After running the go install hello command, run ls $ GOPATH / bin, you can see that hello is already in the bin directory, at this time, if you run the hello command in any directory of the system, it will print hello, world
$ pwd
$ HOME / go / src / hello
$ go install hello
$ ls $ HOME / bin
hello
$ cd / Users /
$ hello
hello, world!
Delete go
When you need to delete go
Delete go in the / usr / local directory
Delete the bin in the PATH environment variable
Delete the bin setting in / etc / profile or $ HOME / .profile or $ HOME / .bahs_profile
If it is installed through the mac os x installation package, then the /etc/paths.d/go file should be deleted
go command
go run: run go source program
go build: compile go source code
go install: Go source code is compiled and packaged into the $ GOPATH / bin directory
Environment variable setting under extension point mac
Add / usr / local / go / bin to the PATH environment variable, you can add the following line to / etc / profile (for a system-wide installation) or $ HOME / .profile
export PATH = $ PATH: / usr / local / go / bin

If the profile file is in / etc / profile, it indicates that it is installed system-wide, mine is under / etc / profile

MacOS loading order of bash shell environment variables

Mac generally uses bash as the default shell, and the environment variables of the mac system are loaded in the following order
System level
/ etc / profile
Read in when logging in, the default setting file
This file sets the environment variables for each user of the system. When the user logs in for the first time, the file is executed and the shell settings are collected from the configuration file in the /etc/profile.d directory
Modification method
If there is no special instructions, the syntax for setting PATH is
export PATH = $ PATH: <PATH 1>: <PATH 2> ... <PATH N>
When there are multiple paths, use a colon to separate each path
/ etc / bashrc
This is an environment variable that must be loaded when bash starts, as a global environment variable setting is feasible
Every user who runs the bash shell executes this file. When the bash shell is opened, the file is read
The modification method is the same as above
/ etc / paths
This is a system-wide path, it is not recommended to make direct changes
Modification method
Loading path
1. Create a file
sudo touch /etc/paths.d/mysql
2. Open this file with vim (if it is opened with open -t, editing is not allowed)
sudo vim /etc/path.d/mysql
3. Edit the file, type and save (close the terminal window and reopen one, then use the MySQL command)
/usr.local/mysql/bin
Or like below
sudo -s ‘echo" / usr / local / sbin / mypath "> /etc/paths.d/mysql’
User-level
/.bash_profile (for mac)
After login, load after / etc / profile is loaded, a very important configuration file
/.bash_login
After login, if ~ / .bash_profile does not exist, load this file
/.profile
After logging in, this file is only loaded if ~ / .bash_login does not exist
/.bashrc (for Linux)
Load when the bash shell is opened
When there is .bash_profile or .bash_login under your $ HOME, the .profile will be ignored.
System level, user level only needs to choose one to modify
Further reading

Mac boot file location
First of all, you need to know what kind of shell you are using Mac OS X, use the command echo $ SHELL
If output csh or tcsh, then use c shell
If the output is bash, sh, zsh, then a variant of Bourne Shell is used
mac os x 10.2 used c shell by default
After mac os x 10.3, the Borune shell is used
If you are using borune shell
Add the environment variables you want to add to the .profile or .bash_profile in your home directory. If the profile or bash_profile exists, you can add the environment variables directly. If the profile or bash_profile file does not exist, it does not matter. Just add the environment
Mac configuration environment variables
/ etc / profile (It is recommended not to modify this file)
Global (public) configuration, no matter which user, it will read the file when logging in
/ etc / bashrc (generally add system-level environment variables in this file)
Global (public) configuration, when the bash shell is executed, this file will be read no matter what method it is
~ / .bash_profile (Generally add user-level environment variables in this file)
Each user can use this file to enter shell information dedicated to their own use. When the user logs in, the file is only executed once
Other extension points
The result of running $ HOME under mac is as follows
yangdeMacBook-Pro-2: ~ yangtao $ $ HOME
-bash: / Users / yangtao: is a directory
yangdeMacBook-Pro-2: ~ yangtao $
So the value of $ HOME is / Users / yangtao, which is the home directory of the currently logged in user

Install, use and delete go under Mac OS X

Related Article

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.