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--
How to configure Go applications, I believe that many inexperienced people do not know what to do, so this article summarizes the causes of the problem and solutions, through this article I hope you can solve this problem.
Go is becoming the programming language of the process.
Each programming language has its own syntax or structure and its own style, which is what makes each language unique. The Go language is no exception, it is easy and elegant in its own way, making software development more interesting.
Go program structure
To understand the structure of the Go program, first write the Go program, and let's re-examine our favorite Hello,World! program.
Package main import "fmt" func main () {fmt.Println ("Hello, World!")}
Each source file is structured with a .go extension, and the first line of the header must be declared as a package. In the above helloworld.go file, the package main on the first line tells the go compiler that the source file will contain the entry main (main function) function of go. This combination of package declaration and main function makes it a separate executable go program.
In the above code, the string is printed to the console, which is standard output. To do this, we import the fmt package using the import keyword. Fmt package is a synthesis of format, which comes with the Go standard library.
The fmt package provides a number of options for writing standard output. Next we will explain the syntax of importing multiple packages separately.
Run the Go program
We use go run or go build to run the relevant Go program. Use these commands to tell the Go compiler to run the Go program.
Run hello-world.go using go
We can also use the go build command, which will generate an executable called hello-world, which can be run independently of go like any other executable file.
At some point, we may want to change the name of the executable, which can be done using the go build-o command, which creates an executable binary. For example:
$go build-o exe # being in the hello-world directory
$. / exe # running the executable Hello,World!
There are times when we want our applications to be installed. We use the go install command. This command creates a binary executable and stores it in the $GOPATH/bin directory.
We need to make sure that GOBIN is added to the system's PATH environment variable so that the binary can be run anywhere on the system.
Code comment
Adding code comments in the right place is as important as writing maintainable code.
In Go, annotations are not only a way to add associative explanations, but also a good way to record API. We will see them when we discuss functions.
There are two types of code comments: line comments and block comments.
1. Line comments, starting with / /, followed by the commented-out text
two。 Block comments place text between / * and * /. Here are two examples:
/ / package main defines the entry point
Package main
/ / import the 'fmt' package from standard library
Import "fmt"
/ *
The main function is the entry point in a Go program.
The main function does not have a return type.
Also, it does not accept any parameters.
, /
Func main () {
Fmt.Println ("Hello, World!")
}
Why is there no semicolon?
As you observed in the code above, there is no single semicolon in our code. Well, that's because behind the scenes Go did it for you. Like C, Go's syntax uses semicolons to terminate statements, but they don't have to appear in the source code. The following is what the Go document says:
Like C, the formal syntax of Go uses semicolons to terminate statements, but unlike C, these semicolons do not appear in the source statement. Instead, lexical analyzers use a simple rule to automatically insert semicolons when scanning, so most of the input text does not have a semicolon.
The rule goes like this: if the last tag before the newline character is an identifier (including words of classes int and float64), a basic text, such as a numeric or string constant, or one of the tags break continue fallthrough return + +--)}
The lexical analyzer always inserts a semicolon after the token. This can be summarized as "if the newline character is marked to end the statement, insert a semicolon."
Semicolons can also be omitted before closing parentheses, so statements such as tokens break continue fallthrough return +--)} tokens break continue fallthrough return + +--)}
No semicolon is required.
Tokens break continue fallthrough return + +--)}
To read more about the inside story, continue to take some time to read Go Docs.
Re-code code
As mentioned earlier, each stand-alone application must have a package main declaration, and the .go file with this declaration must have a main function. This main function is the entry point for our application, just like other programming languages, such as CpGen Java Magi C # and so on.
The difference is that in Java or C #, the main function can accept parameters (string [], args), while in Go the main function accepts no arguments. In addition, the main function does not return anything.
Notice how we import the fmt package: import "fmt". This syntax is valid only if we import a single package. If we are importing multiple packages, we need to group the packages in import (). This is an example:
/ / importing multiple packages in Go import ("context"database/sql"fmt"log")
Note that we haven't put one yet; at the end of any import package, because this will be done for us by the lexical analyzer.
Summary
Go has a very simple program structure, which feels and reads more like C. Following the same pattern, we use main functionality as the entry point for our application. Go provides a convenient way to write clean code and cleverly insert semicolons when needed. The logical description in our code has different types of comments. We'll see later how these comments build documentation for our package.
After reading the above, do you know how to configure Go applications? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!
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.