Go语言文件操作全解析
1. 输入输出格式与栈的使用
在Go语言中,输入格式通常为: number1 ENTER number2 ENTER operator ENTER ,然后显示结果。若用户输入 “q”,程序将停止运行。这里可使用之前开发的栈包。
2. 文件读取
2.1 基础文件读取
在Go里,文件由 os.File 类型的指针表示,也称为文件句柄。标准输入 os.Stdin 和输出 os.Stdout 均为 *os.File 类型。以下是一个读取文件的示例代码:
package main
import (
"bufio"
"fmt"
"io"
"os"
)
func main() {
inputFile, inputError := os.Open("input.dat")
if inputError != nil {
fmt.Printf("An error occurred on opening the inputfile\n" +
"Does the file exist?\n" +
"Have you got acces to it?\n")
return
}
defer inputFile.Close()
inputReader :=
Go语言文件操作全面解析
超级会员免费看
订阅专栏 解锁全文
511

被折叠的 条评论
为什么被折叠?



