Golang学习笔记_06——变量和常量
Golang学习笔记_07——基本类型
Golang学习笔记_08——For循环
if
在Go语言中,if
条件判断语句是用于根据条件表达式的真假来执行不同的代码块。if
语句的基本语法结构相对简单且直观,但也可以包含多个条件和分支。
1. 基本结构
if condition {
// 当 condition 为 true 时执行的代码块
}
1.1 示例
func sqrt(x float64) string {
if x < 0 {
return sqrt(-x) + "i"
}
fmt.Println("sqrt:", math.Sqrt(x))
return fmt.Sprint(math.Sqrt(x))
}
测试方法
func Test_sqrt(t *testing.T) {
type args struct {
x float64
}
tests := []struct {
name string
args args
want string
}{
{
name: "Positive number",
args: args{x: 9},
want: "3", // sqrt(9) = 3
},
{
name: "Zero",
args: args{x: 0},
want: "0", // sqrt(0) = 0
},
{
name: "One",
args: args{x: 1},
want: "1", // sqrt(1) = 1
},
{
name: "Fractional number",
args: args{x: 0.25},
want: "0.5", // sqrt(0.25) = 0.5
},
{
name: "Negative",
args: args{x: -1},
want: "1i",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := sqrt(tt.args.x); got != tt.want {
t.Errorf("sqrt() = %v, want %v", got, tt.want)
}
})
}
}
输出结果
=== RUN Test_sqrt
=== RUN Test_sqrt/Positive_number
sqrt: 3
=== RUN Test_sqrt/Zero
sqrt: 0
=== RUN Test_sqrt/One
sqrt: 1
=== RUN Test_sqrt/Fractional_number
sqrt: 0.5
=== RUN Test_sqrt/Negative
sqrt: 1
--- PASS: Test_sqrt (0.00s)
--- PASS: Test_sqrt/Positive_number (0.00s)
--- PASS: Test_sqrt/Zero (0.00s)
--- PASS: Test_sqrt/One (0.00s)
--- PASS: Test_sqrt/Fractional_number (0.00s)
--- PASS: Test_sqrt/Negative (0.00s)
PASS
2. 简短声明
在 if
语句的初始化语句中,可以使用简短声明(:=
)来声明并初始化一个新的变量,该变量的作用域仅限于 if
、else if
和 else
代码块
if x := someFunction(); x > 0 {
// 使用 x
} else {
// x 在这里不可见
}
示例:
func pow(x, n, lim float64) float64 {
if v := math.Pow(x, n); v < lim {
return v
}
return lim
}
测试方法
func Test_pow(t *testing.T) {
type args struct {
x float64
n float64
lim float64
}
tests := []struct {
name string
args args
want float64
}{
{
name: "Positive base, positive exponent, result less than limit",
args: args{x: 2, n: 3, lim: 10},
want: 8, // 2^3 = 8 < 10
},
{
name: "Positive base, positive exponent, result equal to limit",
args: args{x: 2, n: 4, lim: 16},
want: 16, // 2^4 = 16 == 16
},
{
name: "Positive base, positive exponent, result greater than limit",
args: args{x: 3, n: 4, lim: 50},
want: 50, // 3^4 = 81 > 50
},
{
name: "Base is one",
args: args{x: 1, n: 5, lim: 10},
want: 1, // 1^n = 1 for any n
},
{
name: "Exponent is zero",
args: args{x: 5, n: 0, lim: 10},
want: 1, // x^0 = 1 for any x (except x=0)
},
{
name: "Negative base with even exponent",
args: args{x: -2, n: 2, lim: 5},
want: 4, // (-2)^2 = 4 < 5
},
{
name: "Limit is very low",
args: args{x: 2, n: 3, lim: 1},
want: 1, // 2^3 = 8 > 1
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := pow(tt.args.x, tt.args.n, tt.args.lim); got != tt.want {
t.Errorf("pow() = %v, want %v", got, tt.want)
}
})
}
}
测试结果
=== RUN Test_pow
=== RUN Test_pow/Positive_base,_positive_exponent,_result_less_than_limit
return v
v, lim = 8 10
=== RUN Test_pow/Positive_base,_positive_exponent,_result_equal_to_limit
return v
lim = 16
=== RUN Test_pow/Positive_base,_positive_exponent,_result_greater_than_limit
return v
lim = 50
=== RUN Test_pow/Base_is_one
return v
v, lim = 1 10
=== RUN Test_pow/Exponent_is_zero
return v
v, lim = 1 10
=== RUN Test_pow/Negative_base_with_even_exponent
return v
v, lim = 4 5
=== RUN Test_pow/Limit_is_very_low
return v
lim = 1
--- PASS: Test_pow (0.00s)
--- PASS: Test_pow/Positive_base,_positive_exponent,_result_less_than_limit (0.00s)
--- PASS: Test_pow/Positive_base,_positive_exponent,_result_equal_to_limit (0.00s)
--- PASS: Test_pow/Positive_base,_positive_exponent,_result_greater_than_limit (0.00s)
--- PASS: Test_pow/Base_is_one (0.00s)
--- PASS: Test_pow/Exponent_is_zero (0.00s)
--- PASS: Test_pow/Negative_base_with_even_exponent (0.00s)
--- PASS: Test_pow/Limit_is_very_low (0.00s)
PASS
3. if, else, else if,
if condition1 {
// 当 condition1 为 true 时执行的代码块
} else if condition2 {
// 当 condition2 为 true 时执行的代码块
} else {
// 当所有条件都为 false 时执行的代码块
}
示例:
package main
import "fmt"
func main() {
x := 7
if x > 10 {
fmt.Println("x is greater than 10")
} else if x > 5 {
fmt.Println("x is greater than 5 but not greater than 10")
} else {
fmt.Println("x is 5 or less")
}
}
测试方法
func Test_ifElseDemo(t *testing.T) {
type args struct {
num int
}
tests := []struct {
name string
args args
}{
{
name: "Test",
args: args{num: 1},
},
{
name: "Test",
args: args{num: 10},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ifElseDemo(tt.args.num)
})
}
}
输出结果
=== RUN Test_ifElseDemo
=== RUN Test_ifElseDemo/Test
x is 5 or less
=== RUN Test_ifElseDemo/Test#01
x is greater than 5 but not greater than 10
--- PASS: Test_ifElseDemo (0.00s)
--- PASS: Test_ifElseDemo/Test (0.00s)
--- PASS: Test_ifElseDemo/Test#01 (0.00s)
PASS
源码
// if_demo.go 文件
package if_demo
import (
"fmt"
"math"
)
func sqrt(x float64) string {
if x < 0 {
return sqrt(-x) + "i"
}
fmt.Println("sqrt:", math.Sqrt(x))
return fmt.Sprint(math.Sqrt(x))
}
func pow(x, n, lim float64) float64 {
if v := math.Pow(x, n); v < lim {
fmt.Println("return v\nv, lim =", v, lim)
return v
}
fmt.Println("return v\nlim =", lim)
return lim
}
func ifElseDemo(num int) {
x := num
if x > 10 {
fmt.Println("x is greater than 10")
} else if x > 5 {
fmt.Println("x is greater than 5 but not greater than 10")
} else {
fmt.Println("x is 5 or less")
}
}
// if_demo_test.go 文件
package if_demo
import "testing"
func Test_sqrt(t *testing.T) {
type args struct {
x float64
}
tests := []struct {
name string
args args
want string
}{
{
name: "Positive number",
args: args{x: 9},
want: "3", // sqrt(9) = 3
},
{
name: "Zero",
args: args{x: 0},
want: "0", // sqrt(0) = 0
},
{
name: "One",
args: args{x: 1},
want: "1", // sqrt(1) = 1
},
{
name: "Fractional number",
args: args{x: 0.25},
want: "0.5", // sqrt(0.25) = 0.5
},
{
name: "Negative",
args: args{x: -1},
want: "1i",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := sqrt(tt.args.x); got != tt.want {
t.Errorf("sqrt() = %v, want %v", got, tt.want)
}
})
}
}
func Test_pow(t *testing.T) {
type args struct {
x float64
n float64
lim float64
}
tests := []struct {
name string
args args
want float64
}{
{
name: "Positive base, positive exponent, result less than limit",
args: args{x: 2, n: 3, lim: 10},
want: 8, // 2^3 = 8 < 10
},
{
name: "Positive base, positive exponent, result equal to limit",
args: args{x: 2, n: 4, lim: 16},
want: 16, // 2^4 = 16 == 16
},
{
name: "Positive base, positive exponent, result greater than limit",
args: args{x: 3, n: 4, lim: 50},
want: 50, // 3^4 = 81 > 50
},
{
name: "Base is one",
args: args{x: 1, n: 5, lim: 10},
want: 1, // 1^n = 1 for any n
},
{
name: "Exponent is zero",
args: args{x: 5, n: 0, lim: 10},
want: 1, // x^0 = 1 for any x (except x=0)
},
{
name: "Negative base with even exponent",
args: args{x: -2, n: 2, lim: 5},
want: 4, // (-2)^2 = 4 < 5
},
{
name: "Limit is very low",
args: args{x: 2, n: 3, lim: 1},
want: 1, // 2^3 = 8 > 1
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := pow(tt.args.x, tt.args.n, tt.args.lim); got != tt.want {
t.Errorf("pow() = %v, want %v", got, tt.want)
}
})
}
}
func Test_ifElseDemo(t *testing.T) {
type args struct {
num int
}
tests := []struct {
name string
args args
}{
{
name: "Test",
args: args{num: 1},
},
{
name: "Test",
args: args{num: 10},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
ifElseDemo(tt.args.num)
})
}
}