Go Language Basics Learning Note 1

Source: Internet
Author: User

Go Language Basics Learning Note 1

Installation Tutorial I will not write, recommend you download the MSC installation package, because he will automatically help you configure some environment variables, but gopath this working directory environment variables need to be configured

Right-click My Computer, properties, advanced system settings, environment variables, new system variables

Variable name Capital Gopath


The path to your working directory, such as mine, is d:/go if you have multiple working directories, use ";" Semicolon Split

according to the Convention, 3 directories need to be established under Gopath:

bin (holds compiled executable file)
Pkg (storing the package files generated after compilation)
SRC (store project source code)

View environment variables and working directory console input: Go env

C:\>go Env
Set GOARCH=AMD64//cpu schema information
set gobin=//working Directory Bin folder, storing compiled files
Set Gochar=6
set goexe=.exe//The suffix name of the generated executable file
set GOHOSTARCH=AMD64//When using cross-compilation, you need to cross the schema
set gohostos=windows//When cross-compiling is used, you need to cross the system
set goos=windows//The system name of the current system, or it may be Linux,unix
set gopath=//Your working directory
Set gorace=
set Goroot=c:\go installation directory for//go languages
set GOTOOLDIR=C:\GO\PKG\TOOL\WINDOWS_AMD64//Tools catalogue
set CC=GCC//compiler used
Set Gogccflags=-m64-mthreads-fmessage-length=0
Set cxx=g++
Set cgo_enabled=1

Go Common Commands Introduction:

go get: Get the remote package (if you are getting from GitHub above to get the need to install GIT, if it is googlecode above get required to install Hg)

Go run: Run the program directly, display the results of the operation in the console, easy to debug

Go BUILD: Test compile, check if there is a compilation error, if you are executing an executable file compiled source code, will directly generate an executable file

Go FMT: Formatted source code (some Ides are automatically called when saving), generally do not manually use this command, to ensure that the source format is the same

Go install: There are two functions, if your working directory has the calling package file, it will first compile the package file, and then compile the main program source code, the successful EXE program will be placed in the executable directory bin, the compiled package file will be placed under the package Directory pack

go test: Its role is to run the test file, for example, the file name is *_test.go end of the default is the test file, the normal file is not named like this, in the console directly input go test it will automatically test these _test.go end of the file, and generate a result report , if it is pass OK is no error, test success

Go doc: Look at a function, or a package, for example: Go Doc fmt lists all the functions and types in the FMT package file, and if you want to see a single function, do not add spaces to the command, such as Godoc FMT Println to see Println this function, which It will output the description of the function.

You can also use godoc-http=:8080 to build a local 8080-Port Go official website, you can view the description and documentation of some packages, access method: localhost:8080

compile the generated package file with. A as the suffix name

compile the completed executable, remember to copy from the bin directory under the working directory, that is, the bin directory under the parent, because there is a relative path, otherwise you will not be prompted to find the file

In addition, the Go Build command:

the source code of the Go build executable file

only the source code of the package name is called Main to be compiled by the Go Build command, or you can manually specify the generated executable directory for example: Go build-os d:/111.exe test.go will generate a 111.exe file in the D packing directory

If your package name is not main, but it's something else, then it's just a simple bundle file that doesn't produce any files when you use go build, and it produces an executable when your packages name is main.

the recommended development tool here is Sublime3 and install the package control, then install the Gosublime plugin, restart the editor two times after installation is complete .

So, let's learn a go language version of Hello word!

First use the editor to create a hellow.go file under the source directory SRC under the working directory, because we are going to create an executable file, so the first line of the package name is called Main

write the main function, note that the main function has no parameters, there is no return value, and if you want to execute the main function, it must be inside the package main.

Package Mainimport ("fmt") Func main () {  fmt. Println ("Hello word!" ) }


If you are using sublime and have installed the Gosublime plugin, you can press Ctrl+b to execute the plugin after writing the code, and then enter go run Hello.go directly, and you will see the results displayed in the plugin directly.

If you want to execute go build, run go build hello.go, then generate a hello.exe

Go Language Basics Learning Note 1

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.