Go language learning [2] Basic grammar

Code components

Go programs can be composed of multiple tags, which can be keywords, identifiers, constants, strings, and symbols.

In a Go program, a line represents the end of a statement.

If you plan to write multiple statements on the same line, they must be separated manually using " ; ".

Identifiers are defined the same as in C language.

string

String concatenation in Go language can be achieved through +

package main
import "fmt"
func main() {
    
    
    fmt.Println("Google" + "Runoob")
}

In the Go language, spaces are often used to separate identifiers, keywords, operators, and expressions to improve code readability.

Variable declarations in Go must be separated by spaces.

Use spaces between keywords and expressions.

When calling a function, spaces should be used between the function name and the equal sign on the left, and spaces should also be used between parameters.
For example:

result := add(2, 3)

formatting characters

In Go language, use fmt.Sprintf or fmt.Printf to format a string and assign it to a new string:

  • Sprintf generates a formatted string based on the formatting parameters and returns the string.
  • Printf generates a formatted string based on the formatting parameters and writes it to standard output.
    Example:
package main

import (
    "fmt"
)

func main() {
    
    
   // %d 表示整型数字,%s 表示字符串
    var stockcode=123
    var enddate="2020-12-31"
    var url="Code=%d&endDate=%s"
    var target_url=fmt.Sprintf(url,stockcode,enddate)
    fmt.Println(target_url)
}
package main

import (
    "fmt"
)

func main() {
    
    
   // %d 表示整型数字,%s 表示字符串
    var stockcode=123
    var enddate="2020-12-31"
    var url="Code=%d&endDate=%s"
    fmt.Printf(url,stockcode,enddate)
}

type of data

The emergence of data types is to divide data into data with different memory sizes required. When programming, you need to apply for large memory only when you need to use big data, so that you can make full use of the memory.

Anything not listed can be understood according to the data types of C language or python.
Insert image description here

variable

Variables can be accessed by variable name.

Go language variable names consist of letters, numbers, and underscores, and the first character cannot be a number.

The general form of declaring variables is to use the var keyword, and multiple variables can be declared at one time.

var 变量名 类型

For example:

package main
import "fmt"
func main() {
    
    
    var a string = "Runoob"
    fmt.Println(a)

    var b, c int = 1, 2
    fmt.Println(b, c)
}

https://www.runoob.com/go/go-variables.html

variable declaration

The first is to specify the variable type. If it is not initialized, the variable defaults to zero value. The zero value is the value set by the system by default when the variable is not initialized.

The second is to determine the variable type by yourself based on the value.

Third, if the variable has been declared using var and then uses := to declare the variable, a compilation error will occur. The format is:

package main
import "fmt"
func main() {
    
    

    // 声明一个变量并初始化
    var a = "RUNOOB"
    fmt.Println(a)

    // 没有初始化就为零值
    var b int
    fmt.Println(b)

    // bool 零值为 false
    var c bool
    fmt.Println(c)
}

Insert image description here

0 for numeric types (including complex64/128)

Boolean type is false

String is "" (empty string)

intVal := 1 is equivalent to:

var intVal int 
intVal =1 

Another example:
var f string = "Runoob" can be abbreviated as f := "Runoob":

package main

import "fmt"

func main() {
    
    
	var i int
	var f float64
	var b bool
	var s string
	fmt.Printf("%v %v %v %q\n", i, f, b, s)
}

Insert image description here

package main

import "fmt"

func main() {
    
    
	var d = true
	fmt.Println(d)
}

Insert image description here

package main

import "fmt"

func main() {
    
    
	f := "Runoob" // var f string = "Runoob"

	fmt.Println(f)
}

Insert image description here

Multiple variable declaration

Syntax example (not runnable)

//类型相同多个变量, 非全局变量
var vname1, vname2, vname3 type
vname1, vname2, vname3 = v1, v2, v3

var vname1, vname2, vname3 = v1, v2, v3 // 和 python 很像,不需要显示声明类型,自动推断

vname1, vname2, vname3 := v1, v2, v3 // 出现在 := 左侧的变量不应该是已经被声明过的,否则会导致编译错误


// 这种因式分解关键字的写法一般用于声明全局变量
var (
    vname1 v_type1
    vname2 v_type2
)

Runnable code:

package main
import "fmt"

var x, y int
var (  // 这种因式分解关键字的写法一般用于声明全局变量
    a int
    b bool
)

var c, d int = 1, 2
var e, f = 123, "hello"

//这种不带声明格式的只能在函数体中出现
//g, h := 123, "hello"

func main(){
    
    
    g, h := 123, "hello"
    println(x, y, a, b, c, d, e, f, g, h)
}

Insert image description here

Value types and reference types

Refer to C language pointers.

Problems encountered and solutions

1

[{
“resource”: “/d:/coding2022/Go_1/test.go”,
“owner”: “generated_diagnostic_collection_name#1”,
“severity”: 8,
“message”: “gopls was not able to find modules in your workspace.\nWhen outside of GOPATH, gopls needs to know which modules you are working on.\nYou can fix this by opening your workspace to a folder inside a Go module, or\nby using a go.work file to specify multiple modules.\nSee the documentation for more information on setting up your workspace:\nhttps://github.com/golang/tools/blob/master/gopls/doc/workspace.md.”,
“source”: “go list”,
“startLineNumber”: 1,
“startColumn”: 1,
“endLineNumber”: 1,
“endColumn”: 13
}]
https://blog.csdn.net/wsi__/article/details/127348785

Solution to the error report that gopls was not able to find modules in your workspace in vscode

https://blog.donstpast.cn/index.php/archives/123/

Just go to the Go installation directory. Because there is a go.mod file.
Insert image description here
Well, I should learn how to configure a conflict-free workspace with VScode
https://blog.csdn.net/weixin_39775872/article/details/111133360

https://www.yii666.com/blog/226898.html

User settings and workspace settings
VS Code provides two setting methods:

  • User settings: Settings made in this way will be applied to all projects opened by the user;
  • Workspace settings: Workspace refers to a folder opened with VS Code. A hidden folder named .vscode will be created under this folder, which contains VS Code settings that only apply to the current directory. Workspace Space settings will override user settings.

Everyone has their own preferences. When developing with VS Code, they will configure VS Code at the user level according to their own habits.
But when multiple people complete a project together, the project will have certain coding standards, such as: settings when editing a certain language, code indentation, etc. At this time, a separate work space is required for the project. Level settings.

Change the default user settings and workspace settings.
The settings file of VS Code is setting.json. User settings files are saved in the following directory:

-Windows%APPDATA%CodeUsersettings.json

-Linux$HOME/.config/Code/User/settings.json

The files set by the workspace are saved in the .vscode folder of the current directory.

Two ways to modify default settings

  • Use the editor to directly open the setting.json file for settings;
  • Click File > Preferences > Settings in VS Code to open the settings panel for settings;

⭐⭐⭐ https://www.swvq.com/article/detail/5238 ⭐⭐⭐

https://www.jianshu.com/p/d69a06c4dd01

https://www.runoob.com/go/go-variables.html

Error 1

command-line-arguments
.\1.go:34:8: "fmt" imported and not used

Cause of the problem:
The codes of different files in the main package cannot call each other, but other packages can. So actually student.go is not compiled and executed together.

Problem Solved:
Used the online editor https://goplay.tools/

Guess you like

Origin blog.csdn.net/CSDN_YJX/article/details/130774044