In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)05/31 Report--
The content of this article mainly focuses on how to build go programs on the basis of Go. The content of the article is clear and clear. It is very suitable for beginners to learn and is worth reading. Interested friends can follow the editor to read together. I hope you can get something through this article!
Hello world
Start with a simple program and put the following in a test.go file with a random path:
Package main import ("fmt") func main () {fmt.Println ("Hello World")}
Go manages the program as a package, and each Go source code file must declare its own package, just as the above package main declares that its package is the main package.
Every program must have a main package, the main package serves as the compilation entry package for the entire program, and the main () function in the main package serves as the program execution entry.
The import keyword is used to import other packages. After importing a package, you can use the functions in this package in the current file. For example, after the above main package is imported into the fmt package, you can use the function Println () in the fmt package.
You can then compile the test.go file using go's build tool:
$go build test.go
After compilation, an executable binary is generated under the current path: the test.exe file under Windows and the test file under Unix. Since it is an executable file, of course it can be executed directly:
$. / test
"Hello World" will be output.
You can also combine the compile and run steps into one directly through go's run tool:
2$ go run test.go Hello World
Go run does not generate executable binaries, it actually puts the compiled files into a temporary directory, executes it, and automatically cleans up the temporary directory after execution.
About packages and go files
Each go code file can only and must declare one package using the package statement, which means that a file cannot contain multiple packages.
There are two types of packages in Go, or two types of files:
The package that is compiled into an executable file, that is, the file obtained from the compilation of the main package
A package that is compiled into a shared library, as long as the main package or library file is declared in the go program file
Note:
In the official documentation of go, the binary executable of go is called a command, and sometimes the source file of go is called the source file of the command. Executable programs, in contrast to packages, generally exist as "library" files for import rather than execution
The shared library contains some functions, which are more common, so they are put into the shared library to facilitate function reuse. For example, the Println function in the fmt package is used everywhere, and because the fmt package is a standard library (Standary library), anyone can use it.
There are two types of library files: standard libraries and third-party libraries. Standard libraries are placed in the go installation directory when Go is installed ($GOROOT/src/), and third-party libraries are placed under workspace. We'll talk about workspace later.
Shared libraries can be imported by import (for example, fmt packages). Because the import operation is implemented during compilation, the shared library should not contain any output statements.
Go is a strict, or mandatory, specification for library files. It requires that the package name declared by package in the library file must be the same as the directory name, and only one package is allowed in the same directory, but there can be multiple library file fragments in the same directory, but all of these library files must use package to declare its package name as the directory name. For example:
Src/mycode
| |-first.go |-second.go |-third.go |
If all three files are library files, they must and can only declare their packages as mycode using package mycode. When you go build, you merge them. If the declared package name is not mycode,go build, it will be ignored directly.
Of course, it doesn't matter with the main package, it's not a library file, it can be placed anywhere, and there's no requirement for a directory name. However, if you use go install, there are additional requirements, as described below.
Case naming in library files
Go determines whether the attribute allows export by the case of the first letter of the name:
Attributes with uppercase letters are allowed to be exported. Attributes with lowercase letters are not allowed to be exported.
So when a library file is imported, only constants, variables, functions, etc. that begin with uppercase letters in the library file will be exported and can be used in other files.
For example, in the library file abc.go:
2func first () {} func Second () {}
When importing this package, because the first letter of the first () function is lowercase, the outside world cannot use it, so it can only be used in its own package abc.go and is not visible to the outside world. The Second () function that begins with an uppercase letter is imported, so it is available.
Workspace (workspace) Quick View
Setting the path to workspace through the environment variable GOPATH Go programmers generally put their Go code under a workspace, of course, this is not necessary workspace contains one or more version control system repositories (such as git) each warehouse contains one or more package each package consists of one or more Go source files under a single directory, they must declare the directory name as the directory path of their package name package to determine the import path when importing the package
Go is different from other programming languages in organizing projects. Other languages generally have a separate workspace for each project, and workspace is generally bound to the version control repository.
Now set the GOPATH environment variable, assuming / gocode
Echo 'export GOPATH=/gocode' > > / etc/profile.d/gopath.sh
Chmod + x / etc/profile.d/gopath.shsource / etc/profile.d/gopath.sh
Go env GOPATH determines if it is correct:
$go env GOPATH / gocode
Workspace directory structure
Each workspace is a directory that contains at least three directories:
Src: this directory is used to store Go source code files (also known as command source files) bin: this directory is used to store executable commands (that is, binary go programs that are built, also known as command files) pkg: this directory is used to store shared library files (that is, library packages that are non-executable programs after construction, also known as package object files)
The name given in parentheses is a common alias in the official go documentation.
So, create these three directories first
Mkdir-p / gocode/ {src,pkg,bin}
GOPATH and GOROOT environment variables
The GOPATH environment variable specifies the location of workspace, which tells go where to search for go source files / packages, such as the path from which to search for packages and import them when import. The GOROOT environment variable is used to specify where go is installed. When go needs to import a package, it searches for the package from the location set by GOPATH and GOROOT.
The default location is $HOME/go (Unix) or% USERPROFILE\ go% (Windows). You can manually set the path to the GOPATH environment variable to specify the location of the workspace, which can be specified as multiple directories, separated by colons (Unix system) or semicolons (Windows system). Note that it must not be set to the installation directory of go, that is, it cannot be duplicated with the GOROOT environment variable.
For example, under windows, set the d:\ gocode directory to the path of GOPATH:
Setx GOPATH d:\ gocode
Under Unix, set the $HOME/gocode directory to the path of GOPATH:
Mkdir ~ / gocode export GOPATH=~/gocode echo 'GOPATH=~/gocode' > > ~ / .bashrc
The go env or go env GOPATH command can output the currently valid GOPATH path.
$go env | grep GOPATH GOPATH= "/ root/gocode" $go env GOPATH / root/gocode
Go build
First write two go files, one is the executable go file test.go, and the other is the shared library strutils.go, and put them under the src of workspace.
$mkdir-p $GOPATH/src/ {hello,strutils} $tree-C. ├── bin ├── pkg ├── src │ ├── hello │ │ └── test.go │ └── strutils │ └── strutils.go
Note that test.go is deliberately placed in a directory called hello, and can be placed in any non-library file directory under src (for example, it cannot be placed in the strutils directory). The name is not required.
The hello/test.go is as follows:
Package main import ("fmt"strutils") func main () {fmt.Println ("Hello World") fmt.Println (strutils.ToUpperCase ("hello world"))}
The strutils/strutils.go is as follows:
Package strutils import ("strings") func ToUpperCase (s string) string {return strings.ToUpper (s)} func ToLowerCase (s string) string {return strings.ToLower (s)}
Go build can be used for compilation. The package imported by import will be searched during compilation, and the search path will be the src directory under $GOROOT/src and workspace where the standard library is located. It will only generate additional executables and put them in the current directory, not additional library files. It is important to note, however, that the generated executable name may be unexpected:
For example, go to the directory src/hello and compile the files of test.go. The following three build paths can be compiled successfully:
Cd src/hello go build
# the executable file generated is called hello go build.
# the executable file generated is called hello go build test.go
# the executable file generated is called test
The first two are equivalent, and when go build is compiled in the form of a directory, the generated executable file name is the directory name. When go build is compiled as a go code file name, the generated executable program name is the go source file name (remove the suffix .go or add the suffix .exe).
Go install
Go also has a tool called installation for install,go install, which installs files to the appropriate location. Go install compiles first, then saves the compiled binaries to workspace's bin directory, and places the compiled library files (called package object files with the suffix ".a") in the pkg directory.
Note that when go install, you must first enter under $GOPATH/src, and you can only operate on directories, not on specific go files, because go thinks the package and directory names are the same. Assigning a directory name to go install means that the package name is compiled.
For example, if you install test.go under src/hello, because it imports the strutils package, strutils will be installed automatically:
$cd $GOPATH/src $go install hello $tree $GOPATH/ gocode ├── bin │ └── hello # binary file is called hello instead of test ├── pkg │ └── linux_amd64 │ └── strutils.a # library file └── src ├── hello │ └── test.go └── strutils └── strutils.go
You can also install the library files separately:
$rm-rf $GOPATH/bin/* $GOPATH/pkg/* $cd $GOPATH/src $go install strutils / gocode ├── bin ├── pkg │ └── linux_amd64 │ └── strutils.a └── src ├── hello │ └── test.go └── strutils └── strutils.go
If the directory name is omitted, the package under the current directory is installed:
$cd $GOPATH/src/hello $go install
Again, go to the $GOPATH/src directory before go install.
Because go install can install binaries directly to $GOPATH/bin, you can put this directory in the PATH environment variable to facilitate the execution of these binaries.
$export PATH=$PATH: `go env GOPATH` / bin
Suggestions on the specification of building go programs
1. Since all go projects can be placed in the same $GOPATH directory, it is recommended that you set each project directory a little deeper in order to distinguish between the project directory and the library file directory under src.
For example:
Binpkgsrc |-/ first/project |-main.go |-myliba |-a.go |-b.go |-mylibb |-c.go |-d.go |-/ second/project |-main.go |-lib |-a.go |-b.go
When 2.go install, go to the project directory first.
3. The name of the library file (also the directory name) should be reasonable and as short as possible, but try to see the meaning of the name and minimize the chance of name repetition.
For example, the name util is everywhere and can be changed to numutil, nameutil, and so on.
What is gogo is the abbreviation of golang? golang is a static strongly typed, compiled, concurrent and garbage-collected programming language developed by Google. Its syntax is similar to C language, but it does not include functions such as enumeration, exception handling, inheritance, generics, assertions, virtual functions and so on.
Thank you for your reading. I believe you have some understanding of "how to build go programs on the basis of Go". Go ahead and practice it. If you want to know more about it, you can follow the website! The editor will continue to bring you better articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.