社区微信群开通啦,扫一扫抢先加入社区官方微信群
社区微信群
使用 go-qrcode 生成任意内容的二维码是非常方便的
该库的源代码托管在 github 上:
https://github.com/skip2/go-qrcode
get go-qrcode 代码包:
$ go get github.com/skip2/go-qrcode
报错:package github.com/skip2/go-qrcode: exec: "git": executable file not found in %PATH%
代表没有安装 git 或者 git 没有配置环境变量,下载安装git
网站很慢,还可能失败,找度娘要离线包吧
装好后重新打开cmd,执行:
go get github.com/skip2/go-qrcode
成功无返回,但是go path src下应该会有代码包skip2
package main
import (
"fmt"
"os"
"os/exec"
"github.com/skip2/go-qrcode"
)
func main() {
var context string
//fmt.Println(len(os.Args))
if len(os.Args) != 2 {
fmt.Println("please input your context or https:")
fmt.Scanf("%s", &context)
}
if len(os.Args) == 2 {
context = os.Args[1]
}
err := qrcode.WriteFile(context, qrcode.Medium, 256, "qr.png")
if err != nil {
fmt.Println("make error")
os.Exit(0)
} else {
// 调用win conmand 打开图片
err = exec.Command("rundll32", "url.dll,FileProtocolHandler", "qr.png").Start()
if err != nil {
fmt.Println("error, please see qr.png")
os.Exit(0)
}
}
}
func WriteFile(content string, level RecoveryLevel, size int, filename string) error
WriteFile 函数的原型定义如上,它有几个参数,大概意思如下:
具体使用和下载可以参考 https://github.com/skip2/go-qrcode/blob/master/README.md
go-qrcode 库还为我们提供了对二维码的自定义方式,比如我们可以自定义二维码的前景色和背景色等
具体根据需求再调用吧
如果觉得我的文章对您有用,请随意打赏。你的支持将鼓励我继续创作!