目录标题
一、Writing files 写入文件
1. Writing string to a file 写入字符串
package main
import (
"fmt"
"os"
)
func main() {
f, err := os.Create("writing.txt") // 创建文件
fmt.Println(err)
if err != nil { // 查看是否出错
fmt.Println(err)
return
}
l, err := f.WriteString("Hello, World!") // 写入文件
fmt.Println(err)
if err != nil { // 查看是否出错
fmt.Println(err)
f.Close()
return
}
fmt.Println(l, "bytes written successfully")
err = f.Close()
if err != nil { // 查看是否出错
fmt.Println(err)
return
}
}
// <nil>
// <nil>
// 13 bytes written successfully
2. Writing bytes to a file 写入字节
package main
import (
"fmt"
"os"
)
func main() {
f, err := os.Create("bytes")
if err != nil {
fmt.Println(err)
return
}
d2 := []byte{104, 101, 108, 108, 111, 32, 98, 121, 116, 101, 115}
n, err := f.Write(d2)
if err != nil {
fmt.Println(err)
f.Close()
return
}
fmt.Println(n, "Bytes written successfully")
err = f.Close()
if err != nil {
fmt.Println(err)
return
}
}
// 11 Bytes written successfully
3. Writing strings line by line to a file 一行一行写入
package main
import (
"fmt"
"os"
)
func main() {
f, err := os.Create("Lines")
if err != nil {
fmt.Println(err)
f.Close()
return
}
d := []string{"Welcome to the world of Go1.", "Go is a compiled language.", "It is easy to learn Go."}
for _, v := range d {
fmt.Fprintln(f, v) // 使用 fmt.Fprintln() 函数将每个元素写入文件
if err != nil {
fmt.Println(err)
return
}
}
err = f.Close()
if err != nil {
fmt.Println(err)
return
}
fmt.Println("File written successfully")
}
// File written successfully
4. Appending to a file 添加内容
package main
import (
"fmt"
"os"
)
func main() {
f, err := os.OpenFile("lines", os.O_APPEND|os.O_WRONLY, 0644)
if err != nil {
fmt.Println(err)
return
}
newLine := "File handling is easy."
_, err = fmt.Fprintln(f, newLine)
if err != nil {
fmt.Println(err)
f.Close()
return
}
err = f.Close()
if err != nil {
fmt.Println(err)
return
}
fmt.Println("file appended successfully")
}
// File appended successfully
5. Writing to file concurrently 并发写入文件
package main
import (
"fmt"
"math/rand"
"os"
"sync"
)
func produce(data chan int, wg *sync.WaitGroup) {
n := rand.Intn(999) // 随机生成999以内的数字传入通道
data <- n
wg.Done()
}
func consume(data chan int, done chan bool) {
f, err := os.Create("concurrent") // 创建文件
if err != nil {
fmt.Println(err)
return
}
for d := range data {
_, err = fmt.Fprintln(f, d) // 循环添加入文件
if err != nil { // 如果不为nil则写入失败
fmt.Println(err)
f.Close()
done <- false // 传入通道失败
return
}
}
err = f.Close() // 关闭文件
if err != nil {
fmt.Println(err)
done <- false
return
}
done <- true // 关闭通道
}
func main() {
data := make(chan int)
done := make(chan bool)
wg := sync.WaitGroup{}
for i := 0; i < 100; i++ {
wg.Add(1)
go produce(data, &wg)
}
go consume(data, done)
go func() {
wg.Wait()
close(data)
}()
d := <-done
if d {
fmt.Println("File written successfully")
} else {
fmt.Println("File writing failed")
}
}
// File written successfully
读取文件请查看上一篇文章
二、iota 常量生成器
type WeekDay int
const (
Sunday WeekDay = iota
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
)
func main() {
fmt.Println(Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, WeekDay(2))
fmt.Println(WeekDay(0))
day := WeekDay(1)
switch day {
case Sunday:
fmt.Println("今天是星期日")
case Monday:
fmt.Println("今天是星期一")
case Tuesday:
fmt.Println("今天是星期二")
case Wednesday:
fmt.Println("今天是星期三")
case Thursday:
fmt.Println("今天是星期四")
case Friday:
fmt.Println("今天是星期五")
case Saturday:
fmt.Println("今天是星期六")
default:
fmt.Println("无效的星期")
}
}
// 0 1 2 3 4 5 6 2
// 0
// 今天是星期一
技术小白记录学习过程,有错误或不解的地方请指出,如果这篇文章对你有所帮助请点点赞收藏+关注谢谢支持 !!!