doudeng3008 2018-11-10 00:13
浏览 627
已采纳

在Golang热敏打印机设备上写

I have a thermal printer(ESC/POS) already configured on my linux machine and using the terminal command (as root) I can make it print:

echo "Hello!" > /dev/usb/lp0

However, doing the same procedure in golang nothing happens:

package main

import (
    "fmt"
    "os"
)

func main() {
   fmt.Println("Hello Would!")

   f, err := os.Open("/dev/usb/lp0")

   if err != nil {
       panic(err)
   }

   defer f.Close()

   f.Write([]byte("Hello world!"))
}

What am I doing wrong?

  • 写回答

1条回答 默认 最新

  • douzheng5717 2018-11-10 00:50
    关注

    As described in the documentation os.Open() opens a file read-only.

    You would have discovered the problem if you had checked the return from your Write() call. Always check errors. Don't ignore them, even in tiny programs like this; they will give you a clue as to what is wrong.

    To fix the problem, open the device special for writing with os.OpenFile().

    f, err := os.OpenFile("/dev/usb/lp0", os.O_RDWR, 0)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探