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, " ...
随机推荐
- Zookeeper在分布式架构中的应用
Zookeeper 是一个高性能.高可靠的分布式协调系统,是 Google Chubby 的一个开源实现.Zookeeper 能够为分布式应用提供一致性服务,提供的功能包括:配置维护.域名服务.分布式 ...
- 【数论】[素数筛,phi]P3601签到题
题目描述 给出l,r,要求求出\(\sum_{i = l}^r (i - phi[i]) mod 666623333\) \(1\leq l\leq r\leq 10^{12}\),\(r - l \ ...
- DIP大作业---图像分割
数字图像处理课程的大作业,要求如下: 图像分割就是把图像分成若干个特定的.具有独特性质的区域并提出感兴趣目标的技术和过程.它是由图像处理到图像分析的关键步骤.现有的图像分割方法主要分以下几类:基于阈值 ...
- 您使用的私钥格式错误,请检查RSA私钥配置,charset = utf-8 密钥集不存在
支付宝突然报异常 您使用的私钥格式错误,请检查RSA私钥配置,charset = utf-8 经排查:系统日志 System.Security.Cryptography.CryptographicEx ...
- python 获取文件本身的绝对路径
tester.py: import os print (os.path.dirname(__file__)) print (os.path.abspath(__file__)) print (os.p ...
- Mongoose 两个表关联查询aggregate 以及 Mongoose中获取ObjectId
Mongoose 两个表关联查询aggregate 通常两个表关联查询的时候,是一种一对多的关系,比如订单与订单详情就是一对多的关系,一个订单下面有多个商品 数据模拟 首先我们先将数据模拟出来,先选择 ...
- js操作表格、table、
js添加一行.删除一行 let str="<tr>" +"<td>"+a[1]+"</td>" +&qu ...
- Sketch2Code - Transform sketches into HTML using AI
Sketch2Code - Transform sketches into HTML using AI https://sketch2code.azurewebsites.net/generated- ...
- 解决pytesseract.pytesseract.TesseractNotFoundError: tesseract is not installed or it's not in your path问题
解决方案: 找到python的安装路径下的pytesseract: 例如我的是 C:\develop\Python\Lib\site-packages\pytesseract .用文本编辑器打开 ...
- SDN实验---Mininet实验(玩转流表)
一:实验目的 (一)案例目的 (二)实验内容 (三)网络拓扑结构 二:OpenFlow流表实验准备 (一)使用Python设置网络拓扑 --- tree_topo.py from mininet.to ...