Understanding of getting started with Go development (Go official programming guide|Development considerations|Language escape characters|Common errors and solutions in development|Language comments|Specified code style)

1 Go Official Programming Guide

(1) Golang official website https://go.dev/

  1. You can write code on the official website and run go

image.png
2. You can click tour. After choosing the language, let’s study the programming guide
image.png
3. The api interface of the Go standard library: you can query the packages used by Go, including the explanation of the api (each function of each package)

image.png

(2) Golang Chinese website https://studygolang.com/pkgdoc

The source code of these go packages is in the go/src/ directory under the local go sdk installation path.

image.png

Notes on 2Go development

  1. Go source files have the extension "go"
  2. The execution entry point of the Go application is the main() function
  3. Go language is strictly case sensitive
  4. The Go method is composed of one statement after another, ++ does not need a semicolon after each statement ++ (Go language will automatically add a semicolon after each line), which also reflects the simplicity of Golang
  5. The Go compiler compiles line by line, so we write one statement per line. We cannot write multiple statements on the same line, otherwise an error will be reported.
  6. If the variables ++ defined in Go language ++ or the ++import package ++ are not used, the code cannot be compiled and passed.
  7. Braces appear in pairs, and both are indispensable.

Three Go language escape characters (escape char)

1 \t A tab stop to implement the alignment function
2 \n newline character
3 \\ one\
4 \" one"
5 \r A carriage return (output starts from the beginning of the current line, overwriting the previous content)
package main

import "fmt" //fmt包中提供格式化,输出,输入的函数


func main() {
    
    
	//演示转义字符的使用
	fmt.Println("tim\tjack")
	fmt.Println("hello\nworld")
	fmt.Println("E:\\冰封王座\\Warcraft")
	fmt.Println("tom说\"i love you\"")
	// \r 回车,从当前行的最前面开始输出,覆盖掉以前的内容
	fmt.Println("天龙八部雪山飞狐\r张飞")	
}


>go run main.go
tim     jack
hello
world
E:\冰封王座\Warcraft
tom说"i love you"
张飞八部雪山飞狐
package main

import "fmt"

func main() {
	fmt.Println("姓名\t年龄\t籍贯\t住址\njohn\t12\t河北\t北京")
}

**//Note: If you create this file directly in the same directory as the above script, an error will be reported. **Because there are two main functions under the same main package at this time (prompt error reporting duplicate definition). Create a new folder with this script and put it in

image.png


Four common Go development errors and solutions

1The file name or path is wrong

image.png
2 Syntax errors.
If you forget curly braces, quotation marks, or misspell a word, the Go compiler will report a syntax error. Requirement: Try to understand the error message the compiler will report.

Five Notes on Go Language

  1. Line comments
// 注释内容

Vscode shortcut key to comment multiple lines : select multiple lines to be commented, ctrl+/ means comment for the first time, and cancel comment for the second time
2. Block comment (multi-line comment) Block comment nesting is not allowed in the block comment

/*
注释内容
注释内容
注释内容
*/

Six standard coding styles

  1. Go officially recommends using line comments to comment out entire methods and statements.
  2. Correct indentation and white space
    1). Use a tab operation to achieve indentation, and the entire file will move to the right by default. Shift+tab moves to the left as a whole (the indentation is not uniform, you can first shift+tab to the left of each line, and tab together once) 2
    ). Or use gofmt to format (standard format)
    > gofmt main.gojust format the result Output and
    > gofmt -w main.gowrite the formatted result into the file
    3). It is customary to add a space on both sides of the operator. for example:
    var num = 2 + 4 * 5
  3. code style
// 这么写是正确的
package main
import "fmt"

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

//Go语言不允许这么写,是错误的
package main
import "fmt"

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

  1. Line length convention:
    A line must not exceed 80 characters. If it exceeds, please use line breaks to display, and try to keep the format as elegant as possible.
// 如果特别长的话
package main
import "fmt"

func  main() {
 fmt.Println("HelloWorldHelloWorldHelloWorldHelloWorldHelloWorldHelloWorldHelloWorldHelloWorldHelloWorldHelloWorldHelloWorldHelloWorldHelloWorldHelloWorldHelloWorldHelloWorldHelloWorldHelloWorldHelloWorldHelloWorldHelloWorldHelloWorldHelloWorldHelloWorldHelloWorldHelloWorldHelloWorldHelloWorldHelloWorldHelloWorldHelloWorldHelloWorldHelloWorldHelloWorldHelloWorldHelloWorldHelloWorldHelloWorldHelloWorldHelloWorldHelloWorldHelloWorldHelloWorldHelloWorld") 
}
//可以像下面这么写
package main
import "fmt"

func  main() {
 fmt.Println("HelloWorldHelloWorldHelloWorldHelloWorld",
 "HelloWorldHelloWorldHelloWorldHelloWorldHelloWorldHelloWorld",
 "HelloWorldHelloWorldHelloWorldHelloWorldHelloWorldHelloWorldHe",
  "lloWorldHelloWorldHelloWorldHelloWorldHelloWorldHelloWorldHello",
  "WorldHelloWorldHelloWorldHelloWorldHelloWorldHelloWorldHelloWor",
  "ldHelloWorldHelloWorldHelloWorldHelloWorldHelloWorldHelloWorldH",
  "elloWorldHelloWorldHelloWorldHelloWorldHelloWorldHelloWorldHell",
  "oWorldHelloWorldHelloWorld") 
}

Guess you like

Origin blog.csdn.net/weixin_62173811/article/details/130278426