当前位置: 首页 > news >正文

广州网站设计素材手机商城网站模板

广州网站设计素材,手机商城网站模板,电子商务营销,桂林市中考信息网官网问题描述#xff1a; 在go程序中#xff0c;通过执行一个命令启动一个子命令#xff0c;并通过pipe读取子程序的标准输入和输出#xff0c;通过scanner默认按行读取#xff0c;此时如果子程序输出时没有携带’\n’#xff0c;scanner就不会打印输出#xff0c;而是会累…问题描述 在go程序中通过执行一个命令启动一个子命令并通过pipe读取子程序的标准输入和输出通过scanner默认按行读取此时如果子程序输出时没有携带’\n’scanner就不会打印输出而是会累积到缓存buf上限最终被丢弃直到遇到一个\n然后输出所有的内容默认buf缓存上限时65536如果日志打印处还有限制如glog就限制最大的打印字节数为4096那么就会导致日志再次丢失。 解决方法 不适用scanner去按行读取直接读取管道的内容然后设置上限超过时或者遇到’\n’时打印 测试代码 子程序 #include stdio.h #include unistd.hint main() { int count 0;while (1) {fprintf(stderr, %d, count);count (count 1) % 10;usleep(500); // Sleep for 500,000 microseconds (0.5 seconds) }return 0; }主程序 package mainimport (bufiofmtos/execstringslog )func main() {cmd : exec.Command(./test)stdout, err : cmd.StdoutPipe()if err ! nil {fmt.Println(Error creating StdoutPipe:, err)return}cmd.Stderr cmd.Stdouterr cmd.Start()if err ! nil {fmt.Println(Error starting command:, err)return}scanner : bufio.NewScanner(stdout)// scanner.Split(bufio.ScanBytes)// buf : // for scanner.Scan() {// buf scanner.Text()// if strings.Contains(buf, \n) || len(buf) 256 {// log.Printf(%s, buf)// buf // }// }for scanner.Scan() {log.Printf(%s, scanner.Text())}if err : scanner.Err(); err ! nil {fmt.Println(Error reading standard output:, err)}err cmd.Wait()if err ! nil {fmt.Println(Error waiting for command to finish:, err)} } 修改程序 package mainimport (bufiofmtiologos/exec )func getReaderSize(rd io.Reader) {b, ok : rd.(*bufio.Reader)if ok {log.Printf(rd size: %d, b.Size())} else {log.Printf(rd is not bufio.Reader)} }func main() {// Command to executecmd : exec.Command(./test)// Create a pipe to capture the standard output of the commandstdout, err : cmd.StdoutPipe()if err ! nil {fmt.Println(Error creating StdoutPipe:, err)return}cmd.Stderr cmd.Stdout// Start the commanderr cmd.Start()if err ! nil {fmt.Println(Error starting command:, err)return} Create a scanner to read the commands standard output//scanner : bufio.NewScanner(stdout)//scanner.Split(bufio.ScanBytes)// Read and print each line from the output//buf : make([]byte, 256)//bufLen : 0//for scanner.Scan() {// buf[bufLen] scanner.Bytes()[0]// // buf append(buf, scanner.Bytes()...)// bufLen 1// if buf[bufLen-1] \n || bufLen 256 {// log.Printf(%s, string(buf[:bufLen]))// bufLen 0// }//}// Check for errors in scanning//if err : scanner.Err(); err ! nil {// fmt.Println(Error reading standard output:, err)//}// Create a buffered reader to read from the commands stdoutreader : bufio.NewReaderSize(stdout, 256)getReaderSize(stdout)log.Printf(reader size: %d, reader.Size()) Buffer to store incomplete lines//var incompleteLine []byte// Buffer to read chunks of bytes//chunk : make([]byte, 256)////for {// // Read a chunk of bytes// n, err : reader.Read(chunk)// if err ! nil {// break // Break the loop when an error occurs (e.g., when the command finishes)// }//// // Process each byte in the chunk// for i : 0; i n; i {// b : chunk[i]//// // Check for newline or length exceeding 256// if b \n || len(incompleteLine) 256 {// // Print the line// log.Printf(%s, incompleteLine)//// // Reset the incomplete line buffer// incompleteLine nil// } else {// // Add the byte to the incomplete line buffer// incompleteLine append(incompleteLine, b)// }// }//}for {s, err : reader.ReadSlice(\n)if err ! nil err ! bufio.ErrBufferFull {if len(s) 0 {log.Printf(reader err but exist data, reader size: %d, read string size: %d, string: %s, reader.Size(), len(s), string(s))}fmt.Println(Error reader ReadString:, err)break // Break the loop when an error occurs (e.g., when the command finishes)}log.Printf(reader size: %d, read string size: %d, string: %s, reader.Size(), len(s), string(s))}// Wait for the command to finisherr cmd.Wait()if err ! nil {fmt.Println(Error waiting for command to finish:, err)} } benchmark test: package mainimport (strconvstringstesting )func stringTest1() string {var buf stringfor i : 0; i 256; i {buf strconv.Itoa(i)}return buf }func stringTest2() string {var buf strings.Builderfor i : 0; i 256; i {buf.Write([]byte(strconv.Itoa(i)))}return buf.String() }func stringTest3() string {var buf make([]byte, 0)for i : 0; i 256; i {buf append(buf, []byte(strconv.Itoa(i))...)}return string(buf) }func stringTest4() string {var buf make([]byte, 256)for i : 0; i 256; i {buf[i] 1}return string(buf) }func BenchmarkStringTest1(b *testing.B) {for i : 0; i b.N; i {stringTest1()} } func BenchmarkStringTest2(b *testing.B) {for i : 0; i b.N; i {stringTest2()} } func BenchmarkStringTest3(b *testing.B) {for i : 0; i b.N; i {stringTest3()} } func BenchmarkStringTest4(b *testing.B) {for i : 0; i b.N; i {stringTest4()} }benchmark test cmd: go test -bench . -benchmem go test -benchfunction
http://www.zqtcl.cn/news/831239/

相关文章:

  • 网站建设服务公司专业服务海外留学网站建设方案
  • 哈尔滨建站模板搭建马鞍山市网站建设
  • 上海网站设计专注乐云seo个人简介代码网页制作模板
  • 法律网站建设价格做视频周边的网站
  • 京东物流网站地方门户网站源码下载
  • 重庆建设工程信息网站企业宣传片制作公司光年映画
  • 哪家做网站好 成都ktv网络推广方案
  • 网站开发的软件支持哈尔滨最新消息
  • jsp淘宝客网站有限公司怎么注册
  • 香奈儿电子商务网站建设策划书怎样进行网络营销吸引顾客
  • 昆山网站建设费用我们公司想做网络推广
  • 网站建设一般要多少钱网站开发主要步骤
  • 商用图片的网站网络文化经营许可证查询
  • c 高性能网站开发弄一个公司网站需要多少钱
  • 建设部网站招标投标文件网页设计专业公司
  • 使用iis6搭建网站wordpress调用搜索功能
  • 装配式建筑网站生活家装饰
  • 怎样做软件网站建设百度网站认证官网
  • phpcms网站什么网站都能进的浏览器
  • 建设配资网站有要求吗网站建设不一定当地
  • 永兴网站开发智慧门店管理服务平台
  • 网站建设前的市场分析李炎辉网站建设教程
  • 乱起封神是那个网站开发的?广州市建设注册中心网站首页
  • 网站开发配置网络广告的投放技巧
  • wordpress 漫画网站安徽省建设厅八大员报名网站
  • 音乐网站排名建设部证书查询网站
  • 长沙建站挺找有为太极wordpress eshop 教程
  • 郑州平台类网站网站开发常见面试题
  • 城乡建设网站职业查询系统做网站设计的需要什么材料
  • ui做的好看的论坛网站加工制造网