代码拉取完成,页面将自动刷新
package main
// defer is used to ensure that a function call is performed later in
// a program's execution, usually for purposes of cleanup.
import (
"fmt"
"os"
)
func main() {
// Suppose we wanted to create a file, write to it, and then close
// when we're done. Here's how we could do that with defer.
// Immediately after getting a file object with createFile, we defer
// the closing of that file with closeFile. This will be executed at
// the end of the enclosing function(main), after writeFile has finished.
f := createFile("/tmp/defer.txt")
defer closeFile(f)
writeFile(f)
}
func createFile(p string) *os.File {
fmt.Println("creating file", p)
f, err := os.Create(p)
if err != nil {
panic(err)
}
return f
}
func closeFile(f *os.File) {
fmt.Println("closing file")
err := f.Close()
if err != nil {
fmt.Fprintf(os.Stderr, "error: %v\n", err)
os.Exit(1)
}
}
func writeFile(f *os.File) {
fmt.Println("writing file")
fmt.Fprintln(f, "data")
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。