var AppFs = afero.NewMemMapFs()
or
var AppFs = afero.NewOsFs()
AppFs.Open("/tmp/foo")

Chmod(name string, mode os.FileMode) : error
Chown(name string, uid, gid int) : error
Chtimes(name string, atime time.Time, mtime time.Time) : error
Create(name string) : File, error
Mkdir(name string, perm os.FileMode) : error
MkdirAll(path string, perm os.FileMode) : error
Name() : string
Open(name string) : File, error
OpenFile(name string, flag int, perm os.FileMode) : File, error
Remove(name string) : error
RemoveAll(path string) : error
Rename(oldname, newname string) : error
Stat(name string) : os.FileInfo, error
//文件接口和方法
io.Closer
io.Reader
io.ReaderAt
io.Seeker
io.Writer
io.WriterAt

Name() : string
Readdir(count int) : []os.FileInfo, error
Readdirnames(n int) : []string, error
Stat() : os.FileInfo, error
Sync() : error
Truncate(size int64) : error
WriteString(s string) : ret int, err error
//自定义方法
DirExists(path string) (bool, error)
Exists(path string) (bool, error)
FileContainsBytes(filename string, subslice []byte) (bool, error)
GetTempDir(subPath string) string
IsDir(path string) (bool, error)
IsEmpty(path string) (bool, error)
ReadDir(dirname string) ([]os.FileInfo, error)
ReadFile(filename string) ([]byte, error)
SafeWriteReader(path string, r io.Reader) (err error)
TempDir(dir, prefix string) (name string, err error)
TempFile(dir, prefix string) (f File, err error)
Walk(root string, walkFn filepath.WalkFunc) error
WriteFile(filename string, data []byte, perm os.FileMode) error
WriteReader(path string, r io.Reader) (err error)
  • UA
"github.com/mssola/useragent"
ua := useragent.New("Mozilla/5.0 (Linux; U; Android 2.3.7; en-us; Nexus One Build/FRF91) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1")
        fmt.Println(ua.UA())
        fmt.Printf("%v\n", ua.Bot()) 
        fmt.Println(ua.Browser()) //Edge 94.0.992.38
        fmt.Println(ua.Engine()) //引擎 AppleWebKit 537.36
        fmt.Println(ua.OS()) //系统windows10
        fmt.Println(ua.Platform()) //windows/linux
        fmt.Println(ua.Mobile()) //是否移动端
  • 邮件发送 github.com/jordan-wright/email

      from :="logove@qq.com"
      to:=[]string{"yobybx1y@163.com"}
      nickname := "Yoby"
      secret := ""
      host :="smtp.qq.com"
      port := 25 //465是ssl
      isSSL := false //465设置true
      subject:="主题"
      body:="内容是测试"
    
      auth := smtp.PlainAuth("", from, secret, host)
      e := email.NewEmail()
      if nickname != "" {
          e.From = fmt.Sprintf("%s <%s>", nickname, from)
      } else {
          e.From = from
      }
      e.To = to
      e.Subject = subject
      e.HTML = []byte(body)
      var err error
      hostAddr := fmt.Sprintf("%s:%d", host, port)
      if isSSL {
          err = e.SendWithTLS(hostAddr, auth, &tls.Config{ServerName: host})
      } else {
          err = e.Send(hostAddr, auth)
      }
  • 定时 “github.com/go-co-op/gocron/v2”

    s, _ := gocron.NewScheduler()
      j, _ := s.NewJob(
          gocron.DurationJob(
              2*time.Second, //每2秒
          ),
          gocron.NewTask(
              func(a string, b int) {
                  fmt.Println(time.Now())
              },
              "hello",
              1,
          ),
      )
      fmt.Println(j.ID())
      s.Start()
      select {
      case <-time.After(time.Minute):
      }
      s.Shutdown()
  • 定时另一个 github.com/robfig/cron/v3

  • 版本排序

    version "github.com/hashicorp/go-version"
    func verSort(vv []string) []string {
      versions := make([]*version.Version, len(vv))
      for i, raw := range vv {
          v, _ := version.NewVersion(raw)
          versions[i] = v
      }
      sort.Sort(version.Collection(versions))
      ver := make([]string, len(vv))
      for i, s := range versions {
          ver[i] = s.Original()
    
      }
      return ver
    }
    v1, _ := version.NewVersion("1.2")
      v2, _ := version.NewVersion("1.5")
      if v1.LessThan(v2) { //LessThanOrEqual 包括等于 v1.Equal(v2)等于 GreaterThan大于 GreaterThanOrEquald大于等于
          fmt.Println("v1小于v2")
      }
      v1, _ = version.NewVersion("1.0")
      constraints, _ := version.NewConstraint(">= 1.0, < 1.4")
      if constraints.Check(v1) {
          fmt.Printf("v1在1.0-1.4之间,包括1.0")
      }
  • 安全数据类型转换 https://github.com/spf13/cast

    cast.ToString("mayonegg")         // "mayonegg"
    cast.ToString(8)                  // "8"
    cast.ToString(8.31)               // "8.31"
    cast.ToString([]byte("one time")) // "one time"
    cast.ToString(nil)                // ""
    var foo interface{} = "one more time"
    cast.ToString(foo)                // "one more time"
    cast.ToInt(8)                  // 8
    cast.ToInt(8.31)               // 8
    cast.ToInt("8")                // 8
    cast.ToInt(true)               // 1
    cast.ToInt(false)              // 0
    var eight interface{} = 8
    cast.ToInt(eight)              // 8
    cast.ToInt(nil)                // 0
作者:Yoby  创建时间:2024-05-27 01:22
 更新时间:2024-12-05 13:26
上一篇:
下一篇: