当前位置:网站首页>Go quick start of go language (I): the first go program

Go quick start of go language (I): the first go program

2022-06-11 02:58:00 Procedure 15528175269

install Go

Use Go Before the language , First install Go.Go by Linux、Mac、Windows And other platforms provide corresponding installation packages :Download and install - The Go Programming Language, Select the corresponding installation package according to your operating system and click download , Then install according to the guidance process .

With Mac For example , In addition to the official Go Installers , You can also use  Homebrew  Install in the terminal window :

brew install go

After installation , adopt  go version  see Go To verify whether the installation was successful ( The latest version at the time of writing this tutorial is 1.15.7):

Development tool selection

After installing locally Go After the environment , You need to choose a handy development tool to write Go The language code , Currently, the popular development tools are JetBrains Home paid integrated development environment  GoLand :

And Microsoft's free and open source  Visual Studio Code, Use VS Code It needs to be installed Go Expand

For novice , Recommended GoLand, The follow-up course of the Academy will also be Mac + GoLand Demonstrate as a local development environment .

first Go Program

Select the development tools , Next , You can start writing the first Go Language program , Still follow the tradition of programming languages , We from Hello World Start Go Language learning journey .

First , We turn on GoLand IDE, Choose to create a new  hello  project :

stay Location Enter the project path in , The last level of directory is the project name , stay GOROOT Choose the right Go edition ( General default ), I can click Create The button initializes the project , Once the initialization is complete , Will enter GoLand main interface :

If you have used JetBrains Home products , such as PhpStorm、WebStorm、PyCharm、IntelliJ IDEA, Should be very familiar with , I will not introduce this in detail here IDE The layout of .

Next , We need to create a new file in this project to write Go Code ,Go The source code is stored in  .go In the document , So we are  hello  Create a new one in the directory  hello.go  file :

Then write Go The code is as follows :

namely :

package main
import "fmt"
func main() {
    fmt.Println("Hello World!")
}

Okay , A simple print Hello World Of Go The code is written , Let's briefly analyze the meaning of each line of code .

Code reading

and Java similar ,Go Use the package as the basic unit to manage code ( The analogy is PHP The namespace in ), Every Go The beginning of the source code file is a  package  Statement , Indicates that in this file Go The package to which the code belongs . Bag is Go The most basic unit of distribution in a language , It is also the embodiment of dependency in project management .

To generate Go Executable program , You must declare a named  main  My bag , And the package contains a file named  main()  The main function of , The function is Go The starting point of the executable program , This and C Language and Java Language is very much like , Subsequent compilation Go Project procedures should also include  main  The package file starts .Go Linguistic  main() Functions cannot take arguments , You can't define a return value .

After the package declaration , It's a series of  import  sentence , Used to import the package on which the program depends ( It can be compared to PHP Pass through  use  Introduce classes from other namespaces to understand ). Because this sample program uses  Println()  function , So you need to import the  fmt  package .

There is one caveat , And Java and PHP Different , stay Go In language , Do not include packages that are not used in the source code file , otherwise Go The compiler will report compilation errors . This is the same as the following mandatory function left curly bracket  {   And the case rules for function names that will be mentioned later , All of them reflect Go Language is the design philosophy that solves software engineering problems at the language level .

all Go function ( Including the type member functions mentioned in object-oriented programming ) All with keywords  func  start ( This is related to PHP、Java、JavaScript Wait for the language to pass  function  Defining functions is different ). In addition to Go Function , Left curly bracket  {   Must be at the end of the function definition line , You can't start another row , otherwise Go The compiler will report compilation errors :

syntax error: unexpected semicolon or newline before {
   

If it's in GoLand IDE That's what it says in , The error prompt will be displayed directly :

in addition , And Python、JavaScript similar ,Go The program does not require a semicolon after each statement to indicate the end of the statement , This is also with PHP、Java And so on .

Last , The body of the function is simple , It's called  fmt  Provided by the package  Println  Function print 「hello,world」 This line of string ,Go The language can be passed directly through the package name + .  No. refers to the functions defined in the package .

compile & Run the program

After understanding the meaning of the above code , Let's finally compile and run this Go Program , and PHP、Python Different ,Go Language is a compiled static language ( and Java、C equally ), Running Go Before the program , First compile it into a binary executable , We can go through Go Language provides  go build  Command to Go The program compiles , Then directly run the compiled executable file to execute Go Program code :

You can see , Code execution successful , Printed out 「Hello World!」.

Besides , We can also go through  go run  Command to achieve the same effect , This command combines the compile and execute instructions , The corresponding executable file will be executed immediately after compilation to display the execution results :

Okay , About the first Go The program is simply introduced here

原网站

版权声明
本文为[Procedure 15528175269]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/162/202206110230066857.html