解析go.mod文件的Go语言方法

发表时间: 2024-05-14 10:39

解析 go.mod 文件

package mainimport ("fmt""io/ioutil""golang.org/x/mod/modfile")func main() {    goModFilePathData, _ := ioutil.ReadFile("go.mod")    modFile, _ := modfile.Parse("go.mod", goModFilePathData, nil)    fmt.Println("require如下:")    fmt.Println("")    for _, r := range modFile.Require {        fmt.Println(r.Mod.Path + "@" + r.Mod.Version)    }    fmt.Println("------------------------")    fmt.Println("replace如下:")    fmt.Println("")    for _, r := range modFile.Replace {        fmt.Println("老:" + r.Old.Path + "@" + r.Old.Version)        fmt.Println("新:" + r.New.Path + "@" + r.New.Version)        fmt.Println("")    }}
> # go run demo.gorequire如下:github.com/asaskevich/govalidator@v0.0.0-20210307081110-f21760c49a8dgithub.com/asim/go-micro/v3@v3.7.0github.com/cloudflare/tableflip@v1.2.2github.com/davecgh/go-spew@v1.1.1github.com/denisenkom/go-mssqldb@v0.0.0-20200428022330-06a60b6afbbcgithub.com/didip/tollbooth@v4.0.2+incompatiblegithub.com/didip/tollbooth/v6@v6.1.2github.com/erikdubbelboer/gspt@v0.0.0-20210805194459-ce36a5128377github.com/fatih/color@v1.9.0github.com/ggq89/mutualdep@v0.0.0-20190717055015-11718eaee2a3github.com/gin-gonic/gin@v1.7.4github.com/go-redis/redis/v8@v8.11.3github.com/golang/glog@v0.0.0-20210429001901-424d2337a529github.com/golang/protobuf@v1.5.2github.com/gomodule/redigo@v1.8.6github.com/google/uuid@v1.2.0github.com/hashicorp/mdns@v1.0.1github.com/jinzhu/gorm@v1.9.16github.com/joho/godotenv@v1.3.0github.com/json-iterator/go@v1.1.11github.com/kardianos/osext@v0.0.0-20190222173326-2bc1f35cddc0github.com/kardianos/service@v1.2.0github.com/lib/pq@v1.8.0github.com/manucorporat/try@v0.0.0-20170609134256-2a0c6b941d52github.com/mattn/go-sqlite3@v1.14.5github.com/nats-io/nats-server/v2@v2.1.2github.com/nats-io/nats.go@v1.12.0github.com/nsqio/go-nsq@v1.1.0github.com/patrickmn/go-cache@v2.1.0+incompatiblegithub.com/scottkiss/grtm@v0.0.0-20151009105840-1c69ec29e5b2github.com/sevlyar/go-daemon@v0.1.5github.com/shopspring/decimal@v1.3.1github.com/spf13/cast@v1.4.1github.com/tidwall/gjson@v1.10.1github.com/ugorji/go@v1.1.11github.com/urfave/negroni@v1.0.0github.com/xujiajun/nutsdb@v0.6.0github.com/xutao123777/tcppack@v0.0.0-20220316035708-c7ea3a60d187github.com/yedf/dtmcli@v1.0.0go.uber.org/fx@v1.13.1go.uber.org/multierr@v1.6.0golang.org/x/lint@v0.0.0-20210508222113-6edffad5e616golang.org/x/net@v0.0.0-20210805182204-aaa1db679c0dgolang.org/x/sys@v0.0.0-20211216021012-1d35b9e2eb4egolang.org/x/term@v0.0.0-20210220032956-6a3ed077a48dgolang.org/x/tools@v0.1.5google.golang.org/protobuf@v1.27.1gorm.io/driver/mysql@v1.3.2gorm.io/gorm@v1.23.2------------------------replace如下: