天天看點

【Go】導包錯誤package xx is not in GOROOT、build command-line-arguments: cannot find module for path xx報錯項目結構導入方法

報錯

package xx is not in GOROOT
build command-line-arguments: cannot find module for path xx

項目結構

【Go】導包錯誤package xx is not in GOROOT、build command-line-arguments: cannot find module for path xx報錯項目結構導入方法

tips: 子檔案夾和包名一緻更好

FuncHouse.go

package Leetcode

import (
	"math"
	"fmt"
)

func A(){
	fmt.Println("abc")
} 
           

導入方法

1. 在Algorithm項目檔案夾下運作指令,生成一個go.mod檔案:

go mod init Algorithm
           

2. 在

main.go

使用的絕對路徑為import “Alogorithm/Leetcode"導入FuncHouse.go的函數:

package main

import (
	"Algorithm/Leetcode"
	"fmt"
)

func main() {
	fmt.Println(Leetcode.A())
}
           

tips:使用包名進行調用,而不是go檔案名

3. 再次運作main.go成功!

繼續閱讀