基础语法(常量、枚举、判断语句、循环语句,函数)

这篇博客详细介绍了Golang的基础语法,包括使用const定义常量及其特点,利用enum创建枚举类型,深入探讨了if判断和switch语句的用法,如if后的条件无需括号,switch的case可以包含逻辑表达式。循环语句部分,强调了Golang中只有for循环,并讲解了range和break、continue的用法。在函数部分,讨论了函数的定义、命名返回值、多参数返回以及可变参数的使用。此外,还提到了函数作为参数的特性以及访问权限的约定。
摘要由CSDN通过智能技术生成

常量

关键字const定义常量

// 定义多个常量,const必须要赋值
const(
	m, n = "1", 5
	o = true
	p = 3
)

注意: 我们在定义常量的时候,可以选择不指定常量的类型,这样常量的类型就是不固定类型,我们可以自由的改变常量的类型,但是我们一旦在定义常量的时候指定了类型,那么我们的常量类型就固定了。
常量定义后便不可以使用=进行修改了,在语法上就不允许这么操作。

枚举

go语言使用常量来表示枚举类型

func enum1() {
	const (
		// 自增枚举
		a = iota
		b = iota
		c = iota
		d = iota
	)
	fmt.Println(a, b, c, d)// 0 1 2 3
}

func enum2() {
	const (
		// 自增枚举
		a = iota
		b = iota
		c = iota
		d = iota
	)
	const (
		// 再次到iota时e又被重置为0了
		e  = iota
		f
	)

	fmt.Println(a, b, c, d, e, f)// 0 1 2 3 0 1
}

// 枚举
func enums(){
	//iota是一种枚举递增的意思
	const(
		first = iota
		second
		third
		fourth
	)
	fmt.Println(first, second, third, fourth) // 0 1 2 3
}

func enum2() {
	const (
		// 自增枚举
		a = iota
		b, c, d = iota, iota, iota
	)

	fmt.Println(a, b, c, d)// 0 1 1 1
}

枚举时也支持使用匿名常量_。
iota的小应用

//定义b,kb,mb,gb,tb,pb
func save()  {
	const(
		b = 1 << (10 * iota)
		kb
		mb
		gb
		tb
		pb
	)
	fmt.Println(b, kb, mb, gb, tb, pb) // 1 1024 1048576 1073741824 1099511627776 1125899906842624
}

判断结构

if判断

go的条件判断语句中,if后面是不需要加括号的。

func testIf(a int) int {
if a > 100{
return 100
}else if a < 0{
return 0
}else {
return a
}
}
// 文件读写
func fileIo(){
	const filename = "a1.txt"
	//go的读写会返回两个值,读到的内容和err,用两个值来接收
	contents, err := ioutil.ReadFile(filename)
	// 判断是不是是为空
	if err != nil {
		fmt.Println(err)
	}else {
		fmt.Printf("%s\n", contents)
	}

}
// if的简写
func fileIo2(){
	const filename = "a.txt"
	//这里的contents的名称空间只在if的里面,出了if就访问不到了
	if contents, err := ioutil.ReadFile(filename); err != nil {
		fmt.Println(err)
	}else {
		fmt.Printf("%s\n", contents)
	}

}

switch语句

go 的switch…case语句是不需要break的,默认每个case后面就有break,fallthrough是用于case后面不加break的时候用的。

func eval(a, b int, op string) int {
	var result int
	switch op {
	case "+":
		result = a + b
	case "-":
		result = a - b
	case "*":
		result = a * b
	case "/":
		result = a / b
	default:
		// 报错,让程序停下来
		panic("非法运算符:" + op)
	}
    return result
}

go里的switch的case里可以放逻辑表达式。

//switch没有表达式,判断部分就发给到case里进行
func testswitch(score int) string {
	g := ""

	switch {
	case score>100 || score<0:
		panic("非法成绩")
	case score > 90 :
		g = "profact"
	case score > 80:
		g = "good"
	case score > 60:
		g = "just so so"
	default:
		g = "fuck"
	}
	return g
}

循环语句

Golang里面没有while循环,只保留了for循环。

func test1()  {
	for i:=0; i<10; i ++ {
		fmt.Println(i)
	}
}

//没有起始值的for循环
func convertToBin(n int) string {
	result := ""
	for  ; n > 0; n /= 2{
		lsb := n % 2
		result = strconv.Itoa(lsb) + result
	}
	return result
}

//只有终止值的for循环
func printFile(filename string){
	file, err := os.Open(filename)
	if err != nil{
		panic("文件异常")
	}

	scanner := bufio.NewScanner(file)
	for scanner.Scan(){
		fmt.Println(scanner.Text())
	}
}

//while(true),就是死循环,会时常使用
func forever(){
	for{
		fmt.Println("...")
	}
}

循环range

func test2()  {
	s := "jefflike"
	for i, j := range s {
		fmt.Printf("第%d个字母是%c\n", i, j)
	}
}

break与continue的使用与其他语言的语法一致。

函数

func test3(a, b int) (int, int) {
	return a/b, a%b
}

func main() {
	i, i2 := test3(13, 4)
	fmt.Printf("商是%d,余数是%d", i, i2) // 商是3,余数是1
	}

对于返回值我们还可以提供命名。

func test3(a, b int) (int, int) {
	return a/b, a%b
}

func main() {
	m, n := test3(13, 4)
	fmt.Printf("商是%d,余数是%d", m, n) // 商是3,余数是1
	}

返回值命名的另一种写法(这种方法比较隐式,不能直观的看到返回什么值)

func test3(a, b int) (m, n int) {
	m = a/b
	n = a%b
	return
}
func main() {
	m, n := test3(13, 4)
	fmt.Printf("商是%d,余数是%d", m, n) // 商是3,余数是1
	}

在go语言中多参数返回时很常见的,因为很多包都会返回我们预想的结果或者程序异常的error值,比如

//返回多个值的实际使用的方式就是一个返回值存应该接收的参数,另一个返回值接收返回的异常的参数即result和error
func operations1(a,b int, op string) (int, error)  {
	switch op {
	case "+":
		return a+b, nil
	case "-":
		return a-b, nil
	case "*":
		return a*b, nil
	case "/":
		a, _ = div(a, b)//这个地方不可以使用q接收参数的结果
		return a, nil
	default:
		return 0,fmt.Errorf("%s运算符有问题", op)
	}
}

函数的参数除了是基本类型以外还可以是函数

func operation(op func(int, int) int,a, b int) int {
	//打印出op的值
    p := reflect.ValueOf(op).Pointer() // 此时调用的p的指针内容是: 4761920
    opName := runtime.FuncForPC(p).Name()//获取到这个指针的name, 此时调用的p的指针内容是: main.add
    fmt.Println("此时调用的p的指针内容是:", opName)
    return op(a, b)
}


func add(m, n int) int {
	return m + n
}
func main() {
	v := operation(add, 5, 6)
	fmt.Println(v)
}

既然可以使函数名那么这里也可以使一个匿名函数

	//使用匿名函数,go的lambda就是比较极简
	v2 := operation(func(i,j int) int{
		return i+j
	}, 6, 7)// 此时调用的p的指针内容是: main.main.func1,因为我是一个匿名函数,所以在这里我们叫做func1
	fmt.Println(v2)//13

go语言的函数首字母小写代表private,大写代表public。

可变参数

//可变参数列表的操作与使用
func sum(args ...int) int {
	s := 0
	fmt.Println(args)//[1 2 3 4 5]
	//for i:=range args {//遍历args
	//	s += args[i]
	//}
	for i:=0; i<len(args);i++{//遍历args下标
	   s += args[i]
	}
	return s
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值