循环
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")
}
}