帶有緩沖的讀寫拷貝IO
===
帶有緩存讀取文件
~~~
package main
import (
"bufio"
"fmt"
"io"
"os"
)
func main() {
// 獲得文件句柄
file, e := os.OpenFile("./test.file", os.O_RDONLY, 0666)
if e != nil {
panic(e.Error())
}
// 創建讀取器
reader := bufio.NewReader(file)
// 創建緩沖區
bytes := make([]byte, 1024) // 1024為1K
for {
n, e := reader.Read(bytes)
fmt.Printf("%s",bytes[:n])
if e == io.EOF{
break
}
}
}
~~~
# 注意這里只說一次
```
n, e := reader.Read(bytes)
fmt.Printf("%s",bytes[:n]) bytes可能沒有裝滿 就這樣才能不然會出bug
```
帶有緩沖寫入
~~~
func write() {
// 獲取文件句柄 (O_CREATE) 不存在就創建 (O_WRONLY) 寫入 (O_APPEND) 追加
file, e := os.OpenFile("./new.text", os.O_CREATE|os.O_APPEND|os.O_WRONLY, 00666)
defer file.Close()
if e != nil {
panic(e.Error())
}
// 創建寫出器
writer := bufio.NewWriter(file)
_, e = writer.Write([]byte("hello"))
if e != nil {
fmt.Printf(e.Error())
}
writer.Flush() //倒干緩沖區
}
~~~
### copy
同步
~~~
func copy() {
old, e := os.OpenFile("./test.mp4", os.O_RDONLY, 00666)
new, e := os.OpenFile("./new.mp4", os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 00666)
if e != nil {
panic(e.Error())
}
reader := bufio.NewReader(old)
writer := bufio.NewWriter(new)
bytes := make([]byte, 1024)
for {
_, e := reader.Read(bytes)
if e == io.EOF {
break
}else{
writer.Write(bytes)
}
}
defer func() {
writer.Flush()
old.Close()
new.Close()
}()
}
~~~
共同爭搶資源有可能是亂序不推薦(這個根本不能用,亂序的!!!)
~~~
/**
* Created by GoLand
* User: dollarkiller
* Date: 19-6-10
* Time: 下午9:20
* */
package main
import (
"bufio"
"context"
"io"
"os"
"sync"
)
func main() {
ch := make(chan []byte,1024)
ctx, cancel := context.WithCancel(context.TODO())
var wg sync.WaitGroup
wg.Add(2)
go read(ch,cancel,&wg)
go write(ctx,ch,&wg)
wg.Wait()
//time.Sleep(time.Second * 3)
}
func read(ch chan []byte,cancelFunc context.CancelFunc,wg *sync.WaitGroup) {
file, e := os.OpenFile("./test.file", os.O_RDONLY, 00666)
defer func() {
file.Close()
}()
if e != nil {
panic(e.Error())
}
reader := bufio.NewReader(file)
bytes := make([]byte, 1024)
for {
_, e := reader.Read(bytes)
ch <- bytes
if e == io.EOF {
cancelFunc()
break
}
}
wg.Done()
}
func write(ctx context.Context,ch chan []byte,wg *sync.WaitGroup) {
file, e := os.OpenFile("./new.file", os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 00666)
defer func() {
file.Close()
}()
if e != nil {
panic(e.Error())
}
writer := bufio.NewWriter(file)
forloop:
for {
select {
case <-ch:
writer.Write(<-ch)
case <-ctx.Done():
break forloop
}
}
writer.Flush()
wg.Done()
}
~~~
- 初認GOlang Web
- 關于環境配置
- 路由
- 路由進階與目錄架構
- 靜態文件服務器
- 自定義Middleware
- 與MySQL起舞
- 主從模式概念
- 部署主從集群
- 分庫分表
- 補充:事務
- 補充:常用SQL示例
- Template使用
- 一些小的,但是要知道的東西
- 調度任務
- 流控算法
- 鑒權
- JWT鑒權前置知識:加密解密
- session
- 文件上傳與下載
- 帶緩存讀寫拷貝io
- 參考
- 寫好的文件上傳
- 文件下載
- 拓展:秒傳功能實現
- 擴展:分塊上傳和斷點續傳
- 擴展:分塊上傳
- 擴展:斷點續傳
- 擴展:分布式存儲
- 部署ceph集群
- cephAuth
- go操作ceph集群
- 擴展:云存儲
- go操作oss
- 補充:xorm框架
- 命令小結
- 補充:xorm框架高級部分
- 補充
- MongoDB
- 基礎概念
- 簡簡單單NoSql
- 操作集合(Collection)
- 操作文檔(Document)
- 帶條件的文檔 db.find
- 復雜條件抽文檔 db.find
- redis
- redis操作
- go操作redis
- (新增)配置鑒權
- 密碼學
- 文件校驗算法
- 未來課程的安排
- RPC實踐
- 爬蟲
- 正則表達式
- 爬取手機號
- 爬取郵箱
- 爬取超鏈接
- 爬取身份證號
- 并發爬圖片
- 擴展:CICD
- GO實現自動化部署系統
- 國際化支持
- 并發帶來問題的解決
- GOWEB小知識
- Sync包講解
- sync.Pool
- 千萬級WebSocket消息推送
- 微服務入門:開篇
- 路由通訊
- RabbitMQ
- RabbitMQ工作原理和轉發模式
- Dcoker 下 RabbitMQ and Ui
- Go操作RabbitMQ
- 初步微服務
- go-micro
- 補充:consul
- 快速入門
- 補充:consul超時
- 微服務架構
- 微服務架構全景圖
- 服務注冊和發現
- raft協議基本概念
- raft協議leader選舉詳解
- raft協議日志復制詳解
- raft協議safefy詳解
- rpc調用個服務監控
- etcd
- 命令行使用
- Golang操作etcd
- GO操作etcd OP方式 (分布式鎖基礎)
- etcd 分布式集群樂觀鎖
- (新增)鑒權
- 服務注冊
- 服務發現原理
- 選項設計模式介紹
- 基于插件的注冊組建
- 課前知識
- etcd注冊開發1
- ffmpeg
- 2.0新的啟航
- 高可用Mysql
- mysql邏輯架構
- 常見的MySQL高可用方案
- 索引
- MYSQL調優
- 什么影響了MYSQL的性能
- Mysql 服務器參數配置
- Go深入并發
- 基本同步原語
- 擴展同步原語
- 原子操作
- M P G 模型
- 簡單的消息總線
- GoMicro入門
- GO任務池編寫
- GO依賴注入
- 一些補充
- golang defer在什么時候執行
- 分布式理論篇(面試吹牛必備)
- CAP理論
- Raft協議
- 保證注冊中心的可靠性
- 鏈路追蹤
- 怎么實現強一致性