1 Package Main2 3 Import (4 "FMT"5 "OS"6 )7 8 Func Main () {9 demo1 ()Ten Demo2 () One Demo3 () A Demo4 () - } - the func demo1 () { -Os. Mkdir ("Astaxie", 0777) -Os. Mkdirall ("Astaxie/test1/test2", 0777) -ERR: = OS. Remove ("Astaxie") + ifErr! =Nil { - FMT. PRINTLN (ERR) + } AOs. RemoveAll ("Astaxie") at } - - func Demo2 () { -UserFile: ="Astaxie.txt" -Fout, err: =OS. Create (UserFile) - defer fout. Close () in ifErr! =Nil { - FMT. Println (UserFile, err) to return + } - forI: = 0; I < 10; i++ { theFout. WriteString ("just a test!\r\n") *Fout. Write ([]byte ("just a test!\r\n")) $ }Panax Notoginseng } - the func Demo3 () { +UserFile: ="Astaxie.txt" AFL, Err: =OS. Open (UserFile) the defer fl. Close () + ifErr! =Nil { - FMT. Println (UserFile, err) $ return $ } -BUF: = Make ([]byte, 1024) - for { theN, _: =fl. Read (BUF) - if0 = =N {Wuyi Break the } - OS. Stdout.write (Buf[:n]) Wu } - } About $ func Demo4 () { -Os. Remove ("Astaxie.txt") -}
Notes Golang File