当前位置:网站首页>Go inside the basic knowledge
Go inside the basic knowledge
2022-08-02 06:57:00 【laugh addiction】
一、函数
1、Function preach and is the default value
2、指针:Within the function to modify the data,Can affect the function outside
3、函数的定义:
func funcname(argslist type) [return_types]{
}
4、函数可以传入多个参数,多个返回值
5、Variable length argument function definition:
func add4(nums ...int){
}
二、go的数据类型
1、变量定义
1、var a int = 1
2、a := 1
2、基本的数据类型
int
float
string
byte
bool
3、nil --》零值
4、指针类型:
1、存放内存地址
2、指针也是有类型的
3、var p *string
定义了一个string指针
pThe address of a string in the
三、go与python的区别:
1、go与pythonIn the module and the difference on the package
go:⼀个⽂The folder can be used as a package,同⼀个 package 内部变量、类型、⽅Method definitions can see each other.
python:Must import module inside the bag,也就是py文件,To use the module inside of variable、方法
2、注意:Most of the declaration in the filepackageWith directory name
3、If the imported package has the same situation,Can give his alias life–》import 别名 “导入路径”
四、模块与包
1、package–》目录:
⼀般来说,⼀个⽂The folder can be used as a package,同⼀个 package 内部变量、类型、⽅Method definitions can see each other
1.同一个packageIs defined in the content Shared(可见的),Each other in the different package used byimport “xxx/xxx/包名”导入
2.How to specify whether is the same package:
同一个目录下、指定的package相同;
3.If the imported package has the same situation,Life can give it a nickname-->import 别名 "导入路径"
2、modules–》Package management mode:
Go 语⾔也有 Public 和 Private 的概念,Granularity is the package.如果类型/接⼝/⽅法/函数/字段的⾸字⺟⼤写,则是 Public的,对其他 package 可⻅,如果⾸字⺟⼩写,则是 Private 的,对其他 package 不可⻅
共有的--》Public:Begin with a capital letter content
私有的--》Private:Content with a lowercase letter
五、流程控制
1、选择结构:
1.1、if–》if/ if else
age := 18
if age < 18 {
fmt.Printf("Kid")
} else {
fmt.Println("Adult")
}
// 可以简写为:
if age := 18; age < 18 {
fmt.Println("Kid")
} else {
fmt.Println("Adult")
}
1.2、switch:用于基于不同条件执行不同动作.
同c,但是不需要break,匹配到某个 case,执行完该 case After defined the behavior of the,The default will not continue to perform down.
// 使用了`type` The keyword defines a new type Gender
type Gender int8
// 使用 const 定义了 MALE 和 FEMALE 2 个常量
// Go Language is no enumeration(enum)的概念,Usually can use constant way to simulate the enumeration.
const (
MALE Gender = 1
FEMALE Gender = 2
)
gender := MALE
switch gender {
case FEMALE:
fmt.Println("female")
case MALE:
fmt.Println("male")
default:
fmt.Println("unknown")
// male
}
If you need to continue to perform,需要使用 fallthrough
switch gender {
case FEMALE:
fmt.Println("female")
fallthrough
case MALE:
fmt.Println("male")
fallthrough
default:
fmt.Println("unknown")
}
// 输出结果
// male
// unknown
2、循环结构
2.1、for循环:
格式:for 初始化数据;条件判断;自增/自减{xxxxx}
sum := 0
for i := 0; i < 10; i++ {
if sum > 50 {
break
}
sum += i
}
fmt.Println(sum)
// 注意这里的i是临时变量,Finished with the recycling
2.2、死循环:for true{}和for{}是一个意思
package main
import "fmt"
func main() {
for true {
fmt.Println("这是无限循环.**\n**");
}
for {
fmt.Println("这是无限循环.**\n**");
}
}
2.3、for range{}:迭代数据
nums := []int{
10, 20, 30, 40}
for i, num := range nums {
fmt.Println(i, num)
}
// 0 10
// 1 20
// 2 30
// 3 40
m2 := map[string]string{
"Sam": "Male",
"Alice": "Female",
}
for key, value := range m2 {
fmt.Println(key, value)
}
// Sam Male
// Alice Female
六、go的结构体
1、结构体:由一系列具有相同类型或不同类型的数据构成的数据集合.
2、结构体的组成
1.数据:各种变量
2.方法:函数
3、结构体定义
需要使用 type 和 struct 语句.struct 语句定义一个新的数据类型,结构体中有一个或多个成员
# 例如:定义结构体 Student,并为 Student 添加 name,age 字段
type Student struct {
name string
age int
}
4、方法定义
Implementation method and implementation of function of the difference between,func 和函数名hello 之间,With this method the corresponding instance name stu 及其类型 *Student,Can be accessed through the instance name of the instance fieldsnameAnd other methods
func (name Student)方法名(参数列表)返回值{
函数体
}
# 为结构体Student实现 hello() 方法
func (stu *Student) hello(person string) string {
return fmt.Sprintf("hello %s, I am %s", person, stu.name)
}
5、结构体的使用
5.1、Instantiated structure syntax format is as follows:
variable_name := structure_variable_type {value1, value2...valuen}
指针结构体:
1、stu = &Student{
name = "cali",age=18}
2、stu = new(Student)
普通结构体
1、stu = Student{
name="cali",age=18}
5.2、案例:
//调用方法通过 实例名.方法名(参数) 的方式.
// 创建一个实例
stu := &Student{
name: "Tom",
}
// 调用实例方法
msg := stu.hello("Jack")
fmt.Println(msg) // hello Jack, I am Tom
// 使用New实例化
stu2 := new(Student)
stu2.name = "Cali"
fmt.Println(stu2.hello("Alice")) // hello Alice, I am , name Given the default value""
6、结构体嵌套
type person struct{
Name string
Age int
Contact struct {
Phone, City string
Code int // 门牌号
}
}
func main() {
a := person{
Name:"Corwien", Age:15}
a.Contact.Phone = "10086"
a.Contact.City = "Guangzhou"
a.Contact.Code = 2007
fmt.Println(a)
}
//结果:
go run struct.go
{
Corwien 15 {
10086 Guangzhou 2007}}
7、结构体的组合
package main
import "fmt"
// Embedded structure as an anonymous field
type human struct {
Sex int
}
type teacher struct {
human // GoWill be embedded into the fields by default as the attribute name,So when the assignment need to write:human: human{Sex: 1}
Name string
Age int
}
type student struct {
human
Name string
Age int
}
func main() {
a := teacher{
Name:"Corwien", Age:25, human: human{
Sex: 1}}
b := student{
Name:"mark", Age:12, human: human{
Sex: 1}}
a.Name = "Jack"
a.Age = 10
// a.human.Sex = 0
a.Sex = 0
fmt.Println(a, b)
}
//结果:
go run struct.go
{
{
0} Jack 10} {
{
1} mark 12}
边栏推荐
- MySql -- 不存在则插入,存在则更新或忽略
- Leetcode周赛304
- 路由规划中级篇
- nodejs的安装和全局配置(超详细哦)
- Luogu mini game Daquan (everyone who uses Luogu must know)
- MySQL (3)
- The virtual reality real estate display system foresees the future decoration effect in advance
- npm、nrm两种方式查看源和切换镜像
- c语言指针运算
- A list of 300+ learning resources compiled by senior engineers of the Tao Department (the latest version in 2021)
猜你喜欢

Technology empowers Lhasa's "lungs", Huawei helps Lalu Wetland Smart Management to protect lucid waters and lush mountains

MySQL驱动jar包的下载--保姆教程

秒杀系统小demo

Nacos注册中心的部署与用法详细介绍

Leading the demand and justifying the HR value - the successful launch of the "Human Resource Leading Model HRLM"

线程基础(一)

zabbix auto-discovery and auto-registration

pytorch基本操作:使用神经网络进行分类任务

npm、cnpm的安装

Nacos installation configuration and single-machine deployment tutorial
随机推荐
[OpenCV from entry to practice] image processing technology [pixel] (the most detailed in the whole network)
C语言操作符详解(2)
View source and switch mirrors in two ways: npm and nrm
odoo field 设置匿名函数domain
Nodejs安装教程
如何进行并发数计算(稳定性测试和压力测试)?
BGP experiment (route reflector, federation, route optimization)
Redis(十二) - Redis消息队列
目标检测重要概念——IOU、感受野、空洞卷积、mAP
MySQL union query (multi-table query)
MySql COUNT statistics function explanation
BGP+MPLS综合实验
MySQL 23道经典面试吊打面试官
VMTK环境配置记录
selenium + robotframework的运行原理
go里面的基本知识
APP special test: traffic test
flex布局(弹性布局)
Leading the demand and justifying the HR value - the successful launch of the "Human Resource Leading Model HRLM"
rhce作业