10-8-evening

main
wpl 2024-10-08 21:00:57 +08:00
parent e400aa77da
commit 65c35e7682
9 changed files with 181 additions and 0 deletions

BIN
day05/file/file.exe Normal file

Binary file not shown.

81
day05/file/main.go Normal file
View File

@ -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()
}

0
day05/fileopen/main.go Normal file
View File

Binary file not shown.

57
day05/filewrite/main.go Normal file
View File

@ -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

1
day05/filewrite/xx.txt Normal file
View File

@ -0,0 +1 @@
hello 沙河

27
day05/get_input/main.go Normal file
View File

@ -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() {
}

15
day05/package/main.go Normal file
View File

@ -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)
}

BIN
day05/package/package.exe Normal file

Binary file not shown.