golang的for循环和while差不多,for循环省略初始条件就是while
死循环:
func forever() { for { fmt.Println("abc") } }
package main import ( "bufio" "fmt" "os" "strconv" ) //整数转换成二进制 func convertToBin(n int) string{ //需要判断n = 0 or n < 0 result := "" for ; n > 0; n /=2 { lsb := n % 2 result = strconv.Itoa(lsb) + result } return result } //死循环 func forever() { for { fmt.Println("abc") } } func readFIle(filename string) { file, err := os.Open(filename) //报错误之后终止程序的运行 if err != nil { panic(err) } scanner := bufio.NewScanner(file) for scanner.Scan() { fmt.Println(scanner.Text()) } } func main() { fmt.Println( convertToBin(12), convertToBin(13), convertToBin(0), ) filename := "abc.txt" readFIle(filename) }