Go基础--05 循环与条件

本文介绍了Go语言中的循环结构,如for循环,并展示了如何使用switch语句进行条件判断,强调了switch在Go语言中可以不加break的特点。同时,通过示例代码解释了在条件判断中如何运用if和switch进行逻辑操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

循环

Go语言仅支持循环关键字for

package loop

import "testing"

func TestWhileLoop(t *testing.T){
	n := 0
	for n < 5{
		t.Log(n)
		n++
	}
}

条件语句

switch 不要加break

package condition

import (
	"runtime"
	"testing"
)

func TestCondition(t *testing.T){
	if a:=1 == 1;a{
		t.Log("success")
	}
}

func TestSwitchCondition(t *testing.T){
	for i:=0;i<5;i++{
		switch i {
		case 0,2:
			t.Log("整数")
		case 1:
			t.Log("0dd")
		default:
			t.Log("it is not 0-3")

		}
	}
}
func TestSwitchCaseCondition(t *testing.T){
	for i:=0;i<5;i++{
		switch {
		case i % 2 == 0:
			t.Log("整数")
		default:
			t.Log("it is not 0-3")

		}
	}
}

func TestOs(t *testing.T){
	switch os := runtime.GOOS;os {
	case "linux":
		t.Log("linux")
	default:
		t.Log("windows")
	}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值