The first program hello world in Go

The first program hello world in Go

Code

package main

import "fmt"

// main
func main() {
	fmt.Println("hello world")
}

Description

  • Although the first program in each language is hello world, some language characteristics can be seen from it.
  • At the beginning of each go file, the package needs to be declared, indicating which package the file belongs to. A package can contain one or more go files
  • An executable program must include a package main and also a func main
  • Import is the same as python, it imports other packages, similar to c++ include
  • fmt is a formatted input and output package in go, Println is a formatted output, the output comes with a newline character, similar to python's print
  • Go language does not end with a semicolon, which is the same as python
  • The left parenthesis of the function body defined in go language must be on the same line as the function declaration. If the left parenthesis starts on a new line, the following error will occur:
syntax error: unexpected semicolon or newline before {
  • The comment symbol of go language is exactly the same as C++, single-line comment //, block comment/**/

Command line in go language

  • Sometimes we have to use the command line when compiling the program. Next we will demonstrate how to use the command line to process go
  • cmd input dir to display the files in the directory: there is only one main.go file
    Insert picture description here

1. The first mode of operation

  • go run
go run main.go

Insert picture description here
Check the current directory at this time and find that there is no executable file generated, so go run will only run, but does not generate executable files

2. The second mode of operation

  • go build
go build main.go

Insert picture description here
Check the directory at this time and you will find the executable file is generated
Insert picture description here
.

main.exe

Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_43891775/article/details/113060009