dongying7667 2018-12-21 05:54
浏览 105
已采纳

在Go中导入自定义包

I checked out this answer and for some reason either I was unable to comprehend properly or it didn't work

Also, Before I start, I got that How we can do it with github but I want to try it without github

To start let's say I have a main.go file

package main

import (
    "fmt"
    "math"
    "subpack"
)

//You Import packages without using comma in Go, rather space or new line
//In VS Code, if you use aren't using the package and run then it will automatically removie it

func main() {
    fmt.Println("hello world")
    //We use math.Floor to round the nunmber
    fmt.Println(math.Floor(2.7))
    fmt.Println(math.Sqrt(16))
    fmt.Println(subpack.Reverse)
}

Notice subpack here, it is the custom package I made. The subpack exsist like this

enter image description here

And contains the following code

package subpack

//If we make this in the same root level of our main it will throw an error

func Reverse(s string) string {
    runes := []rune(s)
    for i, j := 0, len(runes)-1; i < j; i, j = i+1, j-1 {
        runes[i], runes[j] = runes[j], runes[i]
    }
    return string(runes)
}

running our go is throwing following error

cannot find package "subpack" in any of:
        /usr/local/go/src/subpack (from $GOROOT)
        /Users/anilbhatia/go/src/subpack (from $GOPATH)

Question: Is it possible and if yes, How to use custom package without github and without making it in the GO main folder rather by simply referencing the folder containing our go file from the directory we are working.

  • 写回答

2条回答 默认 最新

  • doudi8519 2018-12-21 06:07
    关注

    As the error shows, the compiler cannot find subpack from either

    /usr/local/go/src/subpack (from $GOROOT)

    where the standard library packages (such as fmt, strings) are, or

    /Users/anilbhatia/go/src/subpack (from $GOPATH)

    where user installed/defined packages are.

    To make the import work, you just need to include the relative path of the subpack package (relative to $GOPATH/src) in your main.go

    Suppose your main.go is in /Users/anilbhatia/go/src/parentpack, then its import should be

    import "parentpack/subpack"
    

    If I understand you correctly, you want the caller of subpack (say main.go) to be in an unrelated location of subpack. This actually works out of box. Your main.go could be located anywhere. When you compile it, the compiler sees the import path of parentpack/subpack, and goes to $GOPATH/src and $GOROOT/src to find it.

    For more information about the source code organization and some typical examples, you can run

     go help gopath
    

    in your shell.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式
  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错