首页 > golang > go自定义包引入,报错包找不到的问题
2022
01-04

go自定义包引入,报错包找不到的问题

背景


从github下载了一个项目想运行下,发现找不到自己写的包,一直报错


build command-line-arguments: cannot find module for path xxx


目录结构



stockIndicator

    indicator

       price.go

        macd.go

        rsi.go

        utils.go

   main.go


main.go

package main

import (
   "fmt"
   "stock/indicator"
)

func main() {
   priceList := []indicator.Price{
      {230.00, 300.00, 230.00, 299.00},
      {294.00, 311.50, 290.00, 299.01},
      {299.44, 309.50, 271.50, 301.95},
      {301.95, 328.96, 284.00, 289.41},
      {289.40, 339.87, 240.00, 329.37},
      {329.38, 376.00, 322.01, 371.51},
      {371.48, 485.00, 360.00, 455.86},
      {455.86, 510.00, 387.16, 446.82},
      {446.80, 480.00, 421.18, 423.99},
      {423.99, 489.00, 394.99, 466.44},
      {466.39, 752.00, 460.00, 680.00},
      {680.01, 858.23, 663.05, 765.38},
      {766.79, 798.99, 500.00, 742.15},
      {741.49, 769.99, 650.00, 716.74},
      {716.74, 1035.00, 716.42, 982.99},
      {982.99, 1405.00, 944.00, 1340.03},
      {1340.04, 1422.00, 1089.00, 1308.50},
      {1308.50, 1308.78, 766.95, 1123.19},
      {1124.04, 1168.54, 915.00, 1038.00},
      {1038.85, 1272.88, 992.96, 1119.07},
      {1119.69, 1163.41, 790.01, 858.62},
      {857.54, 885.25, 568.00, 844.32},
      {844.99, 907.75, 778.00, 900.00},
      {899.93, 986.88, 895.00, 954.70},
      {954.90, 962.41, 784.28, 829.98},
      {829.91, 895.50, 808.10, 865.45},
      {865.45, 879.40, 819.01, 829.50},
      {829.50, 830.88, 638.21, 698.78},
      {698.78, 737.44, 571.71, 622.30},
      {622.32, 625.00, 453.53, 575.80},
      {575.80, 578.00, 476.56, 483.30},
      {483.79, 496.13, 367.07, 402.52},
      {402.50, 418.15, 358.00, 377.88},
      {377.65, 432.01, 363.21, 406.37},
      {406.23, 529.79, 405.02, 521.15},
      {521.75, 595.94, 498.15, 585.73},
      {585.88, 712.98, 573.99, 622.31},
      {622.02, 702.00, 600.11, 683.26},
      {683.26, 833.01, 630.00, 821.50},
      {821.04, 841.59, 700.84, 762.59},
      {762.59, 765.44, 623.58, 718.37},
      {718.43, 727.01, 657.00, 714.42},
      {714.39, 723.45, 549.00, 599.37},
      {599.42, 606.63, 508.00, 549.18},
      {549.18, 629.00, 542.63, 586.92},
   }
   for _, price := range priceList {
      indicator.AppendPrice(price)
   }

   indicator.InitMACD(12, 26, 9)
   for i := 1; i <= len(priceList); i++ {
      emaX, emaY, diff, dea, macd := indicator.GetMACD(uint64(i))
      indicator.AppendEmaX(emaX)
      indicator.AppendEmaY(emaY)
      indicator.AppendDiff(diff)
      indicator.AppendDea(dea)
      indicator.AppendMacd(macd)

      macd = indicator.ToFix(macd, 4)
      diff = indicator.ToFix(diff, 4)
      dea = indicator.ToFix(dea, 4)
      fmt.Println(i, macd, diff, dea)
   }

   indicator.InitRSI(14)
   for i := 1; i <= len(priceList); i++ {
      up, down, upEma, downEma, rsi := indicator.GetRSI(uint64(i))
      indicator.AppendUp(up)
      indicator.AppendDown(down)
      indicator.AppendUpEma(upEma)
      indicator.AppendDownEma(downEma)
      indicator.AppendRSI(rsi)

      rsi = indicator.ToFix(rsi, 4)
      fmt.Println(i, rsi)
   }

   indicator.InitKDJ(9, 3, 3)
   for i := 1; i <= len(priceList); i++ {
      rsv, k, d, j := indicator.GetKDJ(uint64(i))
      indicator.AppendRSV(rsv)
      indicator.AppendK(k)
      indicator.AppendD(d)
      indicator.AppendJ(j)

      k = indicator.ToFix(k, 4)
      d = indicator.ToFix(d, 4)
      j = indicator.ToFix(j, 4)
      fmt.Println(i, k, d, j)
   }

   indicator.InitBollinger(20, 2)
   for i := 1; i <= len(priceList); i++ {
      up, mid, down := indicator.GetBollinger(uint64(i))
      indicator.AppendBollingerUp(up)
      indicator.AppendBollingerMid(mid)
      indicator.AppendBollingerDown(down)

      up = indicator.ToFix(up, 4)
      mid = indicator.ToFix(mid, 4)
      down = indicator.ToFix(down, 4)
      fmt.Println(i, up, mid, down)
   }
}


解决方案


从go1.11开始modules就开始支持了,现在都go1.16了,但是看这份代码,还在使用gopath的方式。

找到项目目录,执行 go mod init xxx,这个xxx你可以起一个你喜欢的名字,这个操作是给项目制定modules,xxx 是modules的root

然后,修改import的方式,替换成 import xxx/indicator 这种形式。这是标准的go的URL导入方式。

go mod init stock

#自己新写的包就可以通过下面的方式引入了
import (
   "stock/indicator"
)

再编译执行成功


本文》有 0 条评论

留下一个回复