可以使用 == 运算符来比较两个字符串是否相同。
示例代码:
package main
import "fmt"
func main() {
str1 := "hello"
str2 := "world"
str3 := "hello"
if str1 == str2 {
fmt.Println("str1 and str2 are equal")
} else {
fmt.Println("str1 and str2 are not equal")
}
if str1 == str3 {
fmt.Println("str1 and str3 are equal")
} else {
fmt.Println("str1 and str3 are not equal")
}
}
输出结果:
str1 and str2 are not equal
str1 and str3 are equal