From 0a75d06083d15cb6c812a810b2d0c39e7d0c7891 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Sun, 9 Apr 2023 19:06:07 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E4=BD=BF=E7=94=A8=E7=B3=BB?= =?UTF-8?q?=E7=BB=9F=E9=BB=98=E8=AE=A4=E6=B5=8F=E8=A7=88=E5=99=A8=E6=89=93?= =?UTF-8?q?=E5=BC=80=E8=BF=9E=E6=8E=A5=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- browser.go | 33 +++++++++++++++++++++++++++++++++ init.go | 9 +++++++++ 2 files changed, 42 insertions(+) create mode 100644 browser.go diff --git a/browser.go b/browser.go new file mode 100644 index 0000000..30c22c8 --- /dev/null +++ b/browser.go @@ -0,0 +1,33 @@ +// Package util ... +// +// Description : util ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 2023-04-09 19:02 +package util + +import ( + "fmt" + "os/exec" + "runtime" +) + +type browser struct { + commandTable map[string]string +} + +// Open 使用系统默认浏览器打开链接 +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 19:05 2023/4/9 +func (b *browser) Open(link string) error { + run, ok := b.commandTable[runtime.GOOS] + if !ok { + return fmt.Errorf("don't know how to open things on %s platform", runtime.GOOS) + } + + cmd := exec.Command(run, link) + return cmd.Start() +} diff --git a/init.go b/init.go index 37c36a2..3455b98 100644 --- a/init.go +++ b/init.go @@ -40,6 +40,8 @@ var ( UserAgent *userAgent // Console 控制台输出数据 Console *console + // Browser 浏览器操作实例 + Browser *browser ) func init() { @@ -61,4 +63,11 @@ func init() { list: defaultUserAgentList, } Console = &console{} + Browser = &browser{ + commandTable: map[string]string{ + "windows": "start", + "darwin": "open", + "linux": "xdg-open", + }, + } }