• flag替代库,命令行参数解析 github.com/spf13/pflag
    支持短标记,-i=123 –i=123相同

       var name int
      var str string
      var fl float64
      var bl bool
      pflag.IntVarP(&name, "name", "i", 123, "int类型")
      pflag.StringVarP(&str, "str", "s", "字符串", "字符串")
      pflag.Float64VarP(&fl, "fl", "f", 3.12, "浮点数")
      pflag.BoolVarP(&bl, "b", "b", true, "布尔")
      pflag.Parse()
      fmt.Println(name, str, fl, bl)
  • 配置管理库支持json/toml/yaml github.com/spf13/viper

      viper.SetConfigFile("1.toml") //设置文件路径
      viper.Set("ver", 1.343)
      viper.Set("qq", "9999")
      err := viper.WriteConfig() //写入配置
      if err != nil {
          fmt.Println(err)
      }
    
      err = viper.ReadInConfig() //读取配置
      if err != nil {
          return
      }
      ver := viper.Get("ver") //查找配置
      fmt.Println("ver:", ver)
  • 终端颜色 github.com/fatih/color

    color.Red("红色")
     color.Blue("蓝色")
     color.Green("绿色")
     color.Yellow("黄色")
     color.Cyan("天蓝")
     color.Magenta("紫色")
     color.New(color.BgBlue, color.FgHiRed, color.Bold).Add(color.Underline).Println("背景蓝色,前置红色")
  • spf13 github.com/spf13/cobra

    cobra-cli init --license MIT
    cobra-cli add go 添加功能
    隐藏默认的自动完成
    rootCmd.CompletionOptions.HiddenDefaultCmd = true
    rootCmd.Version = "1.0.0"
    rootCmd.PersistentFlags() 用于全局标志
    rootCmd.Flags() 用于当前命令标志
    ~/.cobra.yaml
    author: Yoby <logove@qq.com>
    year: 2024
    useViper: true
    license:
    header: 测试.
    text: | 
      {{ .copyright }}
      MIT License
      Copyright (c) 2024 yoby
  • 获取系统语言 github.com/Xuanwo/go-locale

lang, _ := locale.Detect()
    fmt.Println(lang.String())
    zh-CN,zh-HK,en-US
  • 进度条 github.com/schollz/progressbar
req, _ := http.NewRequest("GET", "https://dl.google.com/go/go1.14.2.src.tar.gz", nil)
resp, _ := http.DefaultClient.Do(req)
defer resp.Body.Close()

f, _ := os.OpenFile("go1.14.2.src.tar.gz", os.O_CREATE|os.O_WRONLY, 0644)
defer f.Close()

bar := progressbar.DefaultBytes(
    resp.ContentLength,
    "downloading",
)
io.Copy(io.MultiWriter(f, bar), resp.Body)
  • 创建图标和托盘菜单
package main

import (
    "fmt"
    "github.com/energye/systray"
    "github.com/logoove/go/yo"
    "os"
    "runtime"
    "time"
)

func main() {
    fmt.Println(yo.Uniqid("ID"))
    systray.Run(onReady, onExit)

}
func onReady() {
    systray.SetIcon(AddIconImage("p"))
    systray.SetTitle("应用标题")
    systray.SetTooltip("提示信息")
    systray.SetOnClick(func(menu systray.IMenu) {
        fmt.Println("单机")
        menu.ShowMenu()
    })
    systray.SetOnDClick(func(menu systray.IMenu) {
        fmt.Println("双击")
    })
    systray.SetOnRClick(func(menu systray.IMenu) {
        menu.ShowMenu()
    })
    systray.CreateMenu()

    mUpdateBizhi := systray.AddMenuItem("更新壁纸", "")
    mUpdateBizhi.Click(func() {
        fmt.Println(2)
    })
    mSBizhi := systray.AddMenuItem("切换壁纸", "")
    mSBizhi.Click(func() {
        fmt.Println(2)
    })
    mFuwuqi := systray.AddMenuItem("服务器", "")
    mFuwuqi.AddSubMenuItem("启动Caddy", "").Click(func() {

    })
    mFuwuqi.AddSubMenuItemCheckbox("PHP启动", "", false).Click(func() {

    })

    systray.AddSeparator()

    mAbout := systray.AddMenuItem("关于", "")
    mAbout.Click(func() {
        fmt.Println(2)
    })
    mChecked := systray.AddMenuItemCheckbox("开机启动", "", false)
    mChecked.Click(func() {
        if mChecked.Checked() {
            mChecked.Uncheck()
            mChecked.SetTitle("已开机启动")
        } else {
            mChecked.Check()
            mChecked.SetTitle("开机启动")
        }
    })
    mQuit := systray.AddMenuItem("退出", "退出应用")
    mQuit.SetIcon(AddIconImage("p"))
    mQuit.Enable()
    mQuit.Click(func() {
        systray.Quit()
    })
}
func onExit() {
    now := time.Now()
    fmt.Println("退出应用了", now.String())
}

// 加载图片
func AddIconImage(path string) (icoData []byte) {
    if runtime.GOOS == "windows" {
        icoData, _ = os.ReadFile(path + ".ico")
    } else {
        icoData, _ = os.ReadFile(path + ".png")
    }
    return icoData
}
作者:Yoby  创建时间:2024-10-13 01:04
 更新时间:2024-12-05 13:26
上一篇:
下一篇: