go 语言学习笔记
逻辑语句、循环语句和函数
package main import "fmt" func isSuShu(number int) bool { var result bool = true; for temp := 2; temp < number; temp++ { if number%temp == 0{ result = false; break; } } return result; } func getSuShuArray(number int) [] int{ var result []int var nCount int = 0 var flag bool for nCount < 100{ nCount++ flag = true; for temp := 2; temp < nCount; temp++{ if nCount%temp == 0{ flag = false; break; } } if flag == true { result = append(result, nCount) // fmt.Println(nCount, "是素数") } } return result; } func main(){ var nCount int = 0 var flag bool for nCount < 100{ nCount++ flag = true; for temp := 2; temp < nCount; temp++{ if nCount%temp == 0{ flag = false; break; } } if flag == true { fmt.Println(nCount, "是素数") } } // 函数返回值为数组 var nMaxNum int = 100; nums := []int{} nums = getSuShuArray(nMaxNum); fmt.Println("素数集合", nums) // 函数返回值为BOOL var number int = 47; if isSuShu(number) == true { fmt.Println(number, "是素数") }else{ fmt.Println(number, "不是素数") } }