Go chan 结构体 写入文件
chan 需要两个进程,一个写,一个读,是分开的,
package main import (
"bufio"
"fmt"
"math/rand"
"os"
"strings"
"sync"
"time"
) type (
cdnfileinfo struct {
filename string
buf []byte
sync.RWMutex
wrtype int
serr string
}
fileWrok interface {
isDirExtxit(path string) int
Writefile(b *backset)
Readfile(b *backset)
Randfilename(b *backset)
}
fileWorkint interface {
Randfilename(b *backset)
} Workout struct {
fileWrok
}
workInt struct {
fileWorkint
}
backset struct {
ducks chan cdnfileinfo
} ) func (fi *Workout) isDirExtxit(path string) int {
finfo, er := os.Stat(path)
if er != nil {
return 0
}
if finfo.IsDir() {
return 1
} else {
return 2
} }
func (fi *workInt)Randfilename(b *backset) {
for {
duck := cdnfileinfo{
filename: "D:/Text/log" + fmt.Sprint(rand.Intn(999999)) + ".txt",
wrtype: 2,
buf: []byte("添加测试"),
}
fmt.Printf("type:%d filename:%s \n", duck.wrtype, duck.filename)
b.ducks <- duck
time.Sleep(time.Second)
}
} func (fi *Workout) Writefile(b *backset) {
for {
duck, _ := <-b.ducks
pos := strings.LastIndex(duck.filename, "/")
path := duck.filename[0 : pos+1]
switch fi.isDirExtxit(path) {
case 0:
{
er := os.Mkdir(path, os.ModePerm)
if er != nil {
continue
}
}
case 2:
{
continue
} } f, er := os.OpenFile(duck.filename, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0666)
if er != nil { }
fbuf := bufio.NewWriter(f)
fbuf.Write(duck.buf)
fbuf.Flush()
fmt.Printf("type:%d filename:%s \n", duck.wrtype, duck.filename)
}
}
func (fi *Workout) Readfile(b *backset) error {
duck,_:= <- b.ducks
_, er := os.Stat(duck.filename)
if er != nil {
return nil
}
sf, er := os.Open(duck.filename)
if er != nil {
return er
}
rbuf := bufio.NewReader(sf)
n, er := rbuf.Read(duck.buf)
if er != nil {
return nil
}
if n == 0 {
return fmt.Errorf("读取文件为空")
}
return nil
} func main() {
wa := new(sync.WaitGroup) p := new(workInt)
c := new(Workout)
b := &backset{
ducks: make(chan cdnfileinfo),
} wa.Add(2)
go func() {
p.Randfilename(b)
wa.Done()
}()
go func() {
c.Writefile(b)
wa.Done()
}() wa.Wait() }
Go chan 结构体 写入文件的更多相关文章
- Demo02_对结构体进行文件读写_张仕传_作业_
#include <iostream> using namespace std; #define StructArrarySize 5 // 老师数量 #define StudentNum ...
- c++ 读写结构体到文件
可以使用fwrite()将一个结构体写入文件: fwrite(&some_struct,sizeof somestruct,1,fp);对应的fread函数可以再把它读出来,此处fwrite ...
- 68.qq号索引结构体写入内存,并实现快速排序
//两个步骤,第一步读取文件,并且初始化索引结构体,把初始化的索引结构体写入到文件,第二步,读取这个文件到索引结构体 //并对这个结构体进行快速排序,得到顺序的索引,再写入文件 #define _CR ...
- Delphi基础Write写入结构体到文件(使用 file of myrecord就行了,真简单)
program WriteStruct; {$APPTYPE CONSOLE} uses SysUtils; //写入结构体 type TCustomer = record ID: ]; Code: ...
- cdev成员结构体file_operations文件操作结构的分析
struct file_operations{ struct module *owner; // 指向拥有该结构的模块的指针,避免正在操作时被卸载,一般为初始化为THIS_MODULES loff_t ...
- 几年前做家教写的C教程(之五专讲结构体与文件操作)
C语言学习宝典(5) 结构体: 将不同类型的数据组合成为一个有机的整体,这个整体就是一个结构体. 例如: Struct student { Int name; Char sex; Float scor ...
- Linux C Socket编程发送结构体、文件详解及实例
利用Socket发送文件.结构体.数字等,是在Socket编程中经常需要用到的.由于Socket只能发送字符串,所以可以使用发送字符串的方式发送文件.结构体.数字等等. 本文:http://www.c ...
- C语言中FILE是结构体,文件类型的指针
c语言文件类型指针 我们在定义文件类型指针变量后,称作该指针指向该文件,但本质上,它不是指向一个存储文件信息的结构型变量么?那么我们在用各个函数对所谓的“文件指针”进行操作时,本质上是不是函数通过获取 ...
- C语言提高 (5) 第五天 结构体,结构体对齐 文件
1昨日回顾 2作业讲解 3 结构体的基本定义 //1 struct teacher { int id; char name[64]; }; struct teacher t5 = { 5, " ...
随机推荐
- Instance Segmentation with Mask R-CNN and TensorFlow
Back in November, we open-sourced our implementation of Mask R-CNN, and since then it’s been forked ...
- 向量召回 vearch
开源向量召回工具 https://github.com/vearch/vearch 架构
- 分布式系统 与 Google
google 论文 http://duanple.com/?p=170 google 论文与开源 http://duanple.com/?p=1096 分布式系统论文集 https://github. ...
- sql server数据库备份单个表的结构和数据生成脚本【转】
1.使用场景:sql server数据库备份单个表的结构和数据,在我们要修改正式系统的数据的一天或者多条某些数据时候,要执行update语句操作,安全稳健考虑,最好先做好所修改的表的结构和数据备份! ...
- BaiduPCS-Go的安装及使用
BaiduPCS-Go的安装及使用 linux下会提示输入验证码,浏览器打开验证码url,多输入几次 Contents [hide] 一. 软件下载及安装 二. 软件的使用 1. 账号登录与退出 2. ...
- 蓝牙BLE: 蓝牙(BLE)协议栈
蓝牙协议是通信协议的一种,一般而言,我们把某个协议的实现代码称为协议栈(protocol stack),BLE协议栈就是实现低功耗蓝牙协议的代码,理解和掌握BLE协议是实现BLE协议栈的前提.当前的蓝 ...
- hive分区与实际分区文件不匹配导致spark读文件出错的问题解决
先解释下,由于历史原因导致hive中的看到分区比hdfs中的文件夹不匹配,存在hive中分区数有,实际hdfs中无此文件夹. spark中通过sparkSQL读取hive中的该表时,将会出现异常. 解 ...
- Spring源码解析之PropertyPlaceholderHelper(占位符解析器)
Spring源码解析之PropertyPlaceholderHelper(占位符解析器) https://blog.csdn.net/weixin_39471249/article/details/7 ...
- 【转载】 TensorFlow学习——tf.GPUOptions和tf.ConfigProto用法解析
原文地址: https://blog.csdn.net/c20081052/article/details/82345454 ------------------------------------- ...
- Hive的两种操作模式
Hive的客户端操作 Hive的客户端操作 通过JDBC操作Hive 通过Thrift操作Hive 通过JDBC操作Hive 首先 Hive 启动远程服务 hive --service hiveser ...