1 函数高级、 2 包的使用、 3 if-else 、4 循环 、5 switch、 6 数组

发布时间 2023-04-26 16:16:57作者: DYuH

1 函数高级

package main

import "fmt"

// 1 函数的参数和返回值都是类型的一部分,函数可以赋值给一个变量

// test3 函数,接收一个参,参数是函数类型:没有参数没有返回值
// test 有返回值,返回值是个函数:函数有两个参数,一个返回值
//func test3(a func()) func(int, int) int {
//	a()
//	return func(x, y int) int {
//		return x + y
//	}
//}

// 2 类型重命名

// 可以给类型重命名
// 如果 type Myint int  相当于我们新定义了一个类型,叫Myint
// 如果 type Myint = int  只是重命名,没有新定义类型

type MyFunc func(int, int) int
type Myint = int

func test3(a func()) MyFunc {
	a()
	return func(x, y int) int {
		return x + y
	}
}

// 3 函数可变长参数
// 可以传任意长度的int类型参数
func test4(a ...int) {
	fmt.Println(a)      //[3 4 5 6 7 7 8 89 9 99]  切片
	fmt.Printf("%T", a) // 类型是 int类型切片 []int
}

// 4 defer  关键字

func main() {

	//var a Myint = 9
	//var b int = 19
	//fmt.Println(a + b)
	//fmt.Println(6,3,4,5,5,6,76,7,8)
	// 完整定义
	//var f MyFunc = test3(func() {
	//	fmt.Println("被传入的函数")
	//})
	//res := f(10, 19)
	//fmt.Println(res)

	// 3 可变长参数
	//test4(3, 4, 5, 6, 7, 7, 8, 89, 9, 99)

	// 4 defer  延迟调用, 当前函数所有代码都执行完了,再执行defer的内容,先注册,后调用 ,先写的defer后来执行
	//var a = 10
	//defer func(i int) {
	//	fmt.Println(i)
	//	fmt.Println("我很帅")
	//
	//}(a)

	defer fmt.Println("我很帅")
	defer fmt.Println("我很帅222")
	//a = 99
	fmt.Println("我不觉得")
	fmt.Println("我也不觉得")

}

2 包的使用

# python 模块和包
	-模块是一个py文件
    -包是一个文件夹 有 __init__
    
    
    
# go 包 ---》包是在一个文件夹下,这个文件夹下所有go文件的第一行要声明包
	一堆go文件的组合
    
    
# 使用步骤
	-新建文件夹,写多个go文件,包名必须一致
    
    
# 注意点:
'''
// 1  包内部,大写开头,表示导出  变量,函数。。。
// 2  包内部的变量函数,只能定义一次
// 3  包内部的所有东西,在包内部直接使用
// 4 包名可以跟文件夹名不一样,但是一个文件夹下只能有一个包
// 5 导入包,按路径导入,如果不重命名,就是文件夹必须跟包名一样
//如果文件夹跟包名不一样,要重命名,可以命名成任意的,但是我们叫了包名    import lqz "go_day03/utils"
//可以命名成任意的   import qqq "go_day03/utils"
// 以后使用包名.  调用即可
// 6 包内的init函数,可以定义多次,只要导入包,就会依次执行init

// 7 导入包,必须使用,不使用就报错,现在就不用,只想执行init, import _ "go_day03/utils"
// 8 一个文件夹下可以再建文件夹建新的包,各个文件夹直接没有必然联系,只是文件夹层级关系
// 9 使用的go mod模式,从1.11后都是这种模式,项目根路径下会有一个go.mod
'''


# 之前有个go path模式,已经弃用了,它的包导入,不是从项目路径下开始导入,而是从go path 的src路径下路径下开始导入



# gosdk内置包,自定义包和第三方包
	-gin:开一个web服务
    -安装第三方包
    
    
 # 使用gin
	配置代理:七牛云
    	-局部:goland中:GOPROXY=https://goproxy.cn,direct
        -全局:改全局go env
	安装: go get  github.com/gin-gonic/gin
    
    写代码:
    package main
    import "github.com/gin-gonic/gin"

    func main() {
        r := gin.Default()
        r.LoadHTMLGlob("templates/*")
        r.GET("/", func(c *gin.Context) {
            c.JSON(200, gin.H{
                "code":    "100",
                "message": "成功",
            })
        })
        r.GET("/index", func(c *gin.Context) {
            c.HTML(200, "index.html", gin.H{"name": "刘清政", "age": 19})
        })
        r.Run() // listen and serve on 0.0.0.0:8080
    }
    

3 if-else

#基本格式
    if 条件{

    }else if 条件{

    }else{

    }
package main

import (
	"fmt"
)

// if-else

func main() {
	score := 55
	if score >= 90 {
		fmt.Println("优秀")
	} else if score >= 80 && score < 90 {
		fmt.Println("良好")
	} else if score >= 60 {
		fmt.Println("及格")
	} else {
		fmt.Println("不及格")
	}

}

4 循环

# python   while for
# go 只有 for循环

# java  while ,for  do while

# go的for循环能实现while循环的功能
package main

// 循环

func main() {
	// 1 基本语法  for关键字 定义变量i=0;i<10;i++{}  三部分都可以省略,但是一般会保留第二部分,第二部分是条件
	//// 2 循环打印0--9
	//for i := 0; i < 10; i++ {
	//	fmt.Println(i)
	//}
	//fmt.Println(i)  // i的作用域范围只在for内部有效

	// 3 循环打印0--9   省略掉第一部分  分号不能省
	//i := 0
	//for ; i < 10; i++ {
	//	fmt.Println(i)
	//}
	//fmt.Println(i) // 10

	// 4 循环打印0--9   第三部分  分号不能省
	//for i := 0; i < 10; {
	//	fmt.Println(i)
	//	i++
	//}

	// 5 循环打印0--9   省略第一部分和第三部分  分号能省略
	//i := 0
	//for i < 10 {
	//	fmt.Println(i)
	//	i++
	//}

	// 6 for 条件 {}   while 循环
	//for true {
	//	fmt.Println("llll")
	//}

	// 死循环
	//for {
	//	fmt.Println("llll")
	//}

	//7  上面是基于索引的循环,这个案例是基于迭代的
	s := "lqz国中"
	//for i, v := range s {
	//	fmt.Println(i)
	//	fmt.Println(string(v))
	//}
	//for i := 0; i < len(s); i++ {
	//	fmt.Println(string(s[i]))
	//}

}

5 switch

switch 是一个条件语句,用于将表达式的值与可能匹配的选项列表进行比较,并根据匹配情况执行相应的代码块,优雅的替换掉else-if



package main

import "fmt"

func main() {
	// 1 switch 基本使用
	//score := 90
	//switch score {
	//case 90:
	//	fmt.Println("我是90")
	//case 80:
	//	fmt.Println("我是80")
	//case 70:
	//	fmt.Println("我是70")
	//}

	//// 2 default 的使用
	//score := 99
	//switch score {
	//case 90:
	//	fmt.Println("我是90")
	//case 80:
	//	fmt.Println("我是80")
	//case 70:
	//	fmt.Println("我是70")
	//default:
	//	fmt.Println("不知道")
	//}

	// 3 多表达式判断
	//score := 66
	//switch score {
	//case 90, 91, 92, 98, 99:
	//	fmt.Println("我是90")
	//case 80, 88:
	//	fmt.Println("我是80")
	//case 70:
	//	fmt.Println("我是70")
	//default:
	//	fmt.Println("不知道")
	//}

	// 4 无表达式
	//score := 66
	//switch {
	//case score > 90:
	//	fmt.Println("我是90")
	//case score > 80 && score < 90:
	//	fmt.Println("我是80")
	//case score >= 60:
	//	fmt.Println("大于60")
	//default:
	//	fmt.Println("不知道")
	//}

	//5 Fallthrough   默认情况下,每个条件之间完,默认加break,但是也不用加,其他语言要加,其他语言去掉break,会无条件执行下一个case
	// 要无条件执行下一个case,需要使用fallthrough
	score := 99
	switch {
	case score > 90:
		fmt.Println("我是90")

	case score > 80 && score < 90:
		fmt.Println("我是80")
		fallthrough // 无条件执行下一个case
	case score >= 60 && score <= 80:
		fmt.Println("大于60")
		fallthrough
	default:
		fmt.Println("不知道")
	}
}

6 数组

# 数组是同一类型元素的集合。例如,整数集合 5,8,9,79,76 形成一个数组。
# Go 语言中不允许混合不同类型的元素,例如包含字符串和整数的数组
# 数组是连续存储,存储同一个类型的数据结构