"Go Language Lesson 1" Course Study Notes (2)

First glimpse: what is the structure of a Go program?

Create a "hello, world" sample program

  • To write a sample program that can print out "hello, world" in Go language, we only need two simple steps, one is to create a folder, and the other is to start writing and running.
  • Generally speaking, Go does not limit where we store code. It is recommended to create a root folder that can collect all projects (for example: ~/goprojects), and then put all projects in it.
  • For the PowerShell terminal of Linux system, macOS system, and Windows system, use the following command to create the hello world folder: mkdir ~/goprojects , mkdir helloworld.

Write and run your first Go program

  • First, we need to create a source file called main.go.
    • Go source files are always named with short words in all lowercase letters and end with a .go extension.
    • If you want to use multiple words in the name of the source file, we usually directly connect multiple words as the source file name instead of using other separators, such as underscores.
    • We try not to use a combination of more than two words as the file name, otherwise it will be difficult to distinguish.
  • Now, you can open the main.go file you just created and type the following code:
    package main
    
    import "fmt"
    
    func main()  {
          
          
    	fmt.Println("hello, world")
    }
    

Structure of the "hello, world" sample program

  • package main This line of code defines a package package in Go.
    • A package is the basic unit of the Go language, usually named with a single lowercase word, and a Go program is essentially a collection of packages.
    • All Go code has its own package, and here all the code for our "hello, world" example is in a package called main.
    • The main package is a special package in Go, and only one package named main is allowed in the entire Go program.
  • The main code in the main package is a function named main:
    func main() {
          
          
    	fmt.Println("hello, world")
    }
    
    • When you run an executable Go program, all code will start running from this entry function.
    • The Go language has a built-in code style commonly known as the Go community convention, and a tool called Gofmt is provided with the installation package, which can help you automatically format the code into the agreed style.
    • Therefore, as a Go developer, please use Gofmt to format your Go source code before submitting your code.
  • The reason why the main function body can call the Println function of the fmt package is that the
    first letter of the Println function name is capitalized. In the Go language, only identifiers with an uppercase initial letter are exported (Exported) and can be seen by code outside the package; if the initial letter is lowercase, it means that the identifier is limited to the package in which it is declared visible.

How is the program compiled in the Go language?

  • Go is a compiled language, which means that only after you compile a Go program can you deliver the resulting executable to others and run it in an environment where Go is not installed.
  • The Go module build mode was officially introduced in Go version 1.11 to completely solve the problem of complex version dependencies in Go projects. In Go version 1.16, Go module has become the default package dependency management mechanism and Go source code construction mechanism for Go.
  • The core of a Go Module is a file named go.mod, which stores all the information about the module's third-party dependencies.
  • In fact, a module is a collection of packages, and these packages are versioned, released and distributed together with the module. The directory where go.mod resides is what we call the root directory of the module it declares. In addition to manually adding according to the prompts, we can also use the go mod tidy command to let Go tools automatically add related package dependencies.

Guess you like

Origin blog.csdn.net/fangzhan666/article/details/132320576