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, " ...
随机推荐
- Sublime Text 3关闭自动升级提醒
由于种种原因,导致不想升级现有版本的ST3,但是被它的升级提醒弹窗严重骚扰! ||||||||||| 解 决 办 法 ||||||||||| 1.首选项 - 设置 - 用户(快捷键 ❀,)打开“Pre ...
- [linux][c/c++]代码片段02
gcc `pkg-config --cflags gtk+-3.0` -o example-1 example-1.c `pkg-config --libs gtk+-3.0` #include &l ...
- win10系统2分钟睡眠
https://blog.csdn.net/widenstage/article/details/78982722 HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSe ...
- 禁用F12,屏蔽鼠标右击、
$(document).ready(function(){ $(document).bind("contextmenu",function(e){ return false; }) ...
- [Beta阶段]测试报告
[Beta阶段]测试报告 博客目录 测试方法及过程 在正式发布前,为检验后端各接口功能的正确性,后端服务器对压力的耐受程度,以及前端各页面.功能的运行情况,我们对我们的服务器及小程序进行了多种测试.除 ...
- uniapp - 文字收缩展示插件
插件地址:https://ext.dcloud.net.cn/plugin?id=657
- 【深入学习linux】CentOS 7 最小化安装后程序必须安装的组件
centos平台编译环境使用如下指令 安装make: yum -y install gcc automake autoconf libtool make 安装g++: yum install gcc ...
- LiteIDE 设置默认编译输出位置
go build命令默认输出路径是当前工作路径,这个显得比较乱,我习惯于把输出文件放置在项目的build目录下,这样方便查找,已经同步备份的时候排除不必要的文件. go build命令可以指定-o 输 ...
- Android如何屏蔽home键和recent键
最近在做一个项目的时候,进入一个 Activity后需要暂时屏蔽掉home键和recent键(back键可以在onKeyDown里面处理),网上找了半天,都是针对旧版本android的方法,andro ...
- linux系统错误码大全
#define EPERM 1 /* Operation not permitted */ #define ENOENT 2 /* No such file or directory */ #defi ...