Golang Learning Road 2-Basic Understanding (Part 1)


Preface

To learn a language, first understand the basic grammar of Golang, such as variable definition, data type, etc.


1. Definition of variables and constants

1.Variables

A variable is a name given to a memory location to store a value of a specific type.
Common definitions:

// 方式一
name := xxx
// 方式二
var (
name = x
age = 18
)

2.Constant

Constants and variables are just the opposite. Once a constant is declared, its value cannot be changed, such as reassignment.
Generally available when the usage scenario is immutable data.

// 方式一
const name = x
// 方式二
const (
	name = ""
	age = 18
)

Complete code:

package main

import "fmt"

func main() {
    
    
	// 1、变量定义:var
	name := "ppl"

	var name1 = "ppl"

	var name2 string
	name2 = "ppl"

	// 定义多个变量
	a, b := "a", "b"

	// 常量定义:const
	const const1 = 3.14

	const (
		const2 = 3.14
		const3 = 0.618
	)

	fmt.Println(name, name1, name2, a, b)
	fmt.Println(const1, const2, const3)
}

Insert image description here

2.Data type

1 Boolean type: true or false
2 Numeric type: int and floating point type float32, float64
3 String type: string string
4 Derived type: Map type, etc.

func main() {
    
    
	// 1	布尔型: true 或 false
	bool1 := true
	// 2	数字类型:整型 int 和浮点型 float32、float64(没有自动类型提升或转换)
	number := 10
	// 3	字符串类型:字符串string
	name := "ppl"
	// 4	派生类型:Map 类型等等
	Map := make(map[string]string)
	Map ["map"] = "yes"

	fmt.Println(bool1, number, name, Map)
}

2. Self-increasing and self-decreasing

Syntax must be on a single line

package main

import "fmt"

func main() {
    
    
	age := 0
	age++

	//fmt.Println(age++)  不允许,必须单独一行 ++
	fmt.Println(age)

	age--
	fmt.Println(age)
}

Insert image description here


3. Pointer

What are pointers?
A pointer variable points to the memory address of a value, the internal garbage collection mechanism (gc).

1. Use pointers & and *

  • Variable pointer storage address: &name
  • Access value using pointer: *ptr
// 声明实际变量
name := "ppl"
// 声明指针变量且存储地址=&name
ptr := &name
fmt.Println("name:", name)
fmt.Println("name ptr:", ptr)
fmt.Println("name ptr使用指针访问值:", *ptr)

2. Null pointer

Null pointer: nil, a null value such as Python’s None, mysql’s null

// 空指针:nil,既空值如Python的None,mysql的null
var ptrNil *string
// 指针做判断 != 与 ==
if ptrNil == nil {
    
    
	fmt.Println("ptrNil:", ptrNil)
}

3. Pointer complete code

package main

import "fmt"

func main() {
    
    
	// 声明实际变量
	name := "ppl"
	// 声明指针变量且存储地址=&name
	ptr := &name
	fmt.Println("name:", name)
	fmt.Println("name ptr:", ptr)
	fmt.Println("name ptr使用指针访问值:", *ptr)

	// 使用new关键字定义
	name1 := new(string)
	*name1 = "ppl"
	fmt.Println(name1)
	fmt.Println(*name1)
	fmt.Println("strPtr():", strPtr(*name1))

	// 空指针:nil,既空值如Python的None,mysql的null
	var ptrNil *string
	// 指针做判断 != 与 ==
	if ptrNil == nil {
    
    
		fmt.Println("ptrNil:", ptrNil)
	}
}

func strPtr(str string) *string {
    
    
	// 返回 string 的指针
	return &str
}


4. Grammar is not supported

1. Self-increasing and self-decreasing--i and ++i

Correct writing: i++ or i–

2. Does not support memory address (pointer) addition and subtraction

3. The ternary operator is not supported

java:a > b ? a : b
python:a if a > b else b

4. In conditional judgment, only bool false is logically false.

The numbers 0 and nil are not supported as conditional judgment syntax. Only the Boolean type false is supported, unlike Python.


5. String string

The bytes of strings in the go language use UTF-8 encoding to identify Unicode text.
If you need to replace and other operations, you need to combine the strings package. The following are some basic usages:

1. Native output

Use rhetorical question marks: ``

2.len gets the string length

len method: get character length len(xx)

3. Splicing & formatting output

str1 + str2
above detailed code as follows:

package main

import (
	"fmt"
)

func main() {
    
    
	str := "ppl\nPPL"
	// 1.原生输出,使用反问号:``
	str1 := `Use "go help <command>" for more information about a command.
		Additional help topics:
        buildconstraint build constraints
        buildmode       build modes

Use "go help <topic>" for more information about that topic.
`
	fmt.Println(str)
	fmt.Println(str1)

	// 2.len 方法:获取字符长度
	lenStr := len(str)
	fmt.Println("len(str)=", lenStr)

	// 3.拼接&格式化输出
	joinStr := str + "+++join+++join"
	fmt.Println(joinStr)
	fmt.Printf("joinStr=%s", joinStr)
}

Insert image description here

4. String slicing

Left closed right open (including the left, not including the right)

// 4.字符串切片
str11 := "HelloWord"
fmt.Println(str11[:5])	// 打印 Hello (切片0-4的字符)
fmt.Println(str11[5:])	// 打印 Word	 (切片5-最后的字符)
fmt.Println(str11[2:5])	// 打印 第2-第5个字符

Insert image description here

End

Guess you like

Origin blog.csdn.net/qq_42675140/article/details/127476486