diff --git a/day05/file/file.exe b/day05/file/file.exe new file mode 100644 index 0000000..bb467fb Binary files /dev/null and b/day05/file/file.exe differ diff --git a/day05/file/main.go b/day05/file/main.go new file mode 100644 index 0000000..9a21d97 --- /dev/null +++ b/day05/file/main.go @@ -0,0 +1,81 @@ +package main + +import ( + "bufio" + "fmt" + "io" + "io/ioutil" + "os" +) + +// 通过ioutil去读取 +func readFromFileByIoutil() { + ret, err := ioutil.ReadFile("./main.go") + if err != nil { + fmt.Printf("read file error,err:%v\n", err) + return + } + fmt.Println(string(ret)) +} + +// 通过ioutil去读取 +func readFromFileByIoutil() { + file, err := ioutil.ReadFile("./main.go") + if err != nil { + fmt.Printf("read file error: err%v\n", err) + return + } + fmt.Println(file) +} + +// 使用bufio去读取文件 +func readFromFilebyBufio() { + fileObj, err := os.Open("./main.go") + if err != nil { + fmt.Printf("Open file failed,err:%v", err) + } + defer fileObj.Close() + //上述记得关闭文件 + reader := bufio.NewReader(fileObj) + //创建一个用来从文件中读内容的对象 + for { + line, err := reader.ReadString('\n') + if err == io.EOF { + return + } + if err != nil { + fmt.Printf("read line failed,err:%v", err) + return + } + fmt.Print(line) + } +} + +func main() { + // fileObj, err := os.Open("./main.go") + // if err != nil { + // fmt.Printf("open file failed,err:%v", err) + // return + // } + // defer fileObj.Close() + // var tmp [128]byte + // //读文件 + // for { + // n, err := fileObj.Read(tmp[:]) + // if err == io.EOF { + // fmt.Println("读完了") + // return + // } + // if err != nil { + // fmt.Printf("read failed!:%v", err) + // return + // } + // fmt.Printf("读了%d个字节", n) + // fmt.Println(string(tmp[:n])) + // if n < 128 { + // return + // } + // } + // readFromFilebyBufio() + readFromFileByIoutil() +} diff --git a/day05/fileopen/main.go b/day05/fileopen/main.go new file mode 100644 index 0000000..e69de29 diff --git a/day05/filewrite/filewrite.exe b/day05/filewrite/filewrite.exe new file mode 100644 index 0000000..ddcd80b Binary files /dev/null and b/day05/filewrite/filewrite.exe differ diff --git a/day05/filewrite/main.go b/day05/filewrite/main.go new file mode 100644 index 0000000..ea3ef3b --- /dev/null +++ b/day05/filewrite/main.go @@ -0,0 +1,57 @@ +package main + +import ( + "bufio" + "fmt" + "io/ioutil" + "os" +) + +//打开文件 + +// func filewrite() { + +// } + +func main() { + // file, err := os.OpenFile("./xx.txt", os.O_APPEND|os.O_CREATE, 0644) + // if err != nil { + // fmt.Printf("open file failed,err :%v", err) + // return + // } + // // fmt.Println(file) + // //write + // file.Write([]byte("吱吱吱!")) + // file.WriteString("萨瓦迪卡!") + // file.Close() + readfile() + iouitlfile() +} + +func readfile() { + file, err := os.OpenFile("./xx.txt", os.O_WRONLY|os.O_TRUNC, 0666) + if err != nil { + fmt.Printf("open file failed,err :%v", err) + return + } + defer file.Close() + writer := bufio.NewWriter(file) + for i := 0; i < 1; i++ { + writer.WriteString("hello") + } + writer.Flush() //将缓存的数据写入文件 +} + +func iouitlfile() { + str := "hello 沙河" + err := ioutil.WriteFile("./xx.txt", []byte(str), 0666) + //这部分会清空原文件内容,然后再写入数据 + if err != nil { + fmt.Printf("open file failed,err :%v", err) + return + } +} + +//上述通过writestring去实现 +//另外或者可以通过bufio.newWriter去实现 +//再者,可以通过ioutil.Writefile diff --git a/day05/filewrite/xx.txt b/day05/filewrite/xx.txt new file mode 100644 index 0000000..8fa467a --- /dev/null +++ b/day05/filewrite/xx.txt @@ -0,0 +1 @@ +hello 沙河 \ No newline at end of file diff --git a/day05/get_input/main.go b/day05/get_input/main.go new file mode 100644 index 0000000..69cec8b --- /dev/null +++ b/day05/get_input/main.go @@ -0,0 +1,27 @@ +package main + +import ( + "bufio" + "fmt" + "os" +) + +func useScan() { + var s string + fmt.Print("input ur message") + fmt.Scanln(&s) + fmt.Printf("你输入的是%s\n", s) + +} + +func useBufio() { + var s string + reader := bufio.NewReader(os.Stdin) + s, _ = reader.ReadString('\n') + //直到读到了\n换行符为止 + fmt.Printf("您输入的是%s", s) +} + +func main() { + +} diff --git a/day05/package/main.go b/day05/package/main.go new file mode 100644 index 0000000..0cd1982 --- /dev/null +++ b/day05/package/main.go @@ -0,0 +1,15 @@ +package main + +import "fmt" + +var x int8 = 10 + +const pi = 3.14 + +func init() { + fmt.Println(pi) +} + +func main() { + fmt.Println(x) +} diff --git a/day05/package/package.exe b/day05/package/package.exe new file mode 100644 index 0000000..1c99eb4 Binary files /dev/null and b/day05/package/package.exe differ