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, " ...
随机推荐
- luoguP4721 【模板】分治 FFT
P4721 [模板]分治 FFT 链接 luogu 题目描述 给定长度为 \(n-1\) 的数组 \(g[1],g[2],..,g[n-1]\),求 \(f[0],f[1],..,f[n-1]\),其 ...
- Shared Virtual Memory (SVM) Functions
Description Shared Virtual Memory (SVM) (Glossary): An address space exposed to both the host and th ...
- GET /static/css/bootstrap.min.css.map HTTP/1.1" 404
解决办法:删除bootstrap.min.css文件内容最后一行/*…………*/内容即可
- UDF——提取指定线上随时间变化的物理量
Fluent版本:Fluent 19.0 Visual Studio版本:Visual Studio 2013 有时候我们想要实现一些功能,比如:我们在使用Fluent进行瞬态计算的时候,想要获取某条 ...
- 安装ubuntu 16.04版本时搭建环境参考的文章
重新编译的命令:make all ZIP_DEBUGINFO_FILES=0 DISABLE_HOTSPOT_OS_VERSION_CHECK=ok 解决ubuntu中vi不能正常使用方向键与退格键的 ...
- pyqt(day1)
参考代码地址:https://github.com/cxinping/Pyqt5 pyqt在线帮助文档:https://www.riverbankcomputing.com/static/Docs/P ...
- 自顶向下深入分析Netty(六)--Channel总述
自顶向下深入分析Netty(六)--Channel总述 自顶向下深入分析Netty(六)--Channel源码实现 6.1 总述 6.1.1 Channel JDK中的Channel是通讯的载体,而N ...
- java中过滤器(Filter)与拦截器(Interceptor )区别
过滤器(Filter) Servlet中的过滤器Filter是实现了javax.servlet.Filter接口的服务器端程序,主要的用途是设置字符集.控制权限.控制转向.做一些业务逻辑判断等.其工作 ...
- 【Docker】容器与系统时间同步
宿主机时间 [root@slave-1 ~]# date Fri May 12 11:20:30 CST 2017 容器时间 [root@slave-1 ~]# docker exec -ti 879 ...
- layui中select实现二级关联
目的:实现店铺和仓库的二级关联,通过选择不同的店铺,来显示这个门店对应的库位信息. 1. 在select选项上添加lay-filter选择器. <div class="layui-in ...