zoukankan      html  css  js  c++  java
  • Golang中如何开源自己的包?

    1.远端git新建仓库

    2.初始化本地仓库

    git clone https://github.com/XXX/goutils.git
    
    go mod init github.com/XXX/goutils

    3.编写代码

    新建包,例子初始化一个简单处理hash的函数

    package hash
    
    import (
        "crypto/md5"
        "encoding/hex"
        "errors"
        "fmt"
        "io"
        "os"
    )
    
    // get file md5
    func FileMd5(filename string) (string, error) {
        file, err := os.Open(filename)
        if err != nil {
        return "", errors.New(
            fmt.Sprintf("md5.go hash.FileMd5 os open error %v", err))
        }
        h := md5.New()
        _, err = io.Copy(h, file)
        if err != nil {
            return "", errors.New(fmt.Sprintf("md5.go hash.FileMd5 io copy error %v", err))
        }
    
        return hex.EncodeToString(h.Sum(nil)), nil
    }
    
    // get string md5
    func StringMd5(s string) string {
        md5 := md5.New()
        md5.Write([]byte(s))
        return hex.EncodeToString(md5.Sum(nil))
    }

    4.版本发布?(不发好像也行)

    5.开启gomodule=on (如果用的goland,在配置里也要开启)

    go get github.com/XXX/goutils

    6. 新建工程

     go mod init use_goutils

    7.import "github.com/XXX/goutils/hash", 就可以直接用了

  • 相关阅读:
    3、Ubantu下安装nginx
    2、关于mongodb外部访问不成功的问题
    1. libcurl.so.4: cannot open shared object file: No such file or directory
    Php 笔记
    Jade之Plain Text
    Jade之Mixins
    Jade之Interpolation
    Jade之Template Inheritance
    Jade之Includes
    Jade之Filters
  • 原文地址:https://www.cnblogs.com/peterleee/p/15104577.html
Copyright © 2011-2022 走看看