How to get path of imported package

huangapple go评论89阅读模式
英文:

How to get path of imported package

问题

我在尝试获取导入包的路径时遇到了困难。当我在导入的包中打印os.Getwd()的结果时,它显示的是与main包相同的路径。

这是我所做的。

项目结构

How to get path of imported package

lib/lib.go

package lib

import "os"
import "fmt"

func init() {
    dir, _ := os.Getwd()
    fmt.Println("lib.init() :", dir)
}

func GetPath() {
    dir, _ := os.Getwd()
    fmt.Println("lib.GetPath() :", dir)
}

main.go

package main

import "os"
import "fmt"
import "test-import/lib"

func main() {
    dir, _ := os.Getwd()
    fmt.Println("main :", dir)
    lib.GetPath()
}

结果

lib.init() : /Users/novalagung/Documents/go/src/test-import
main : /Users/novalagung/Documents/go/src/test-import
lib.GetPath() : /Users/novalagung/Documents/go/src/test-import

lib/lib.go中的os.Getwd()的结果仍然是与main相同的路径。我想要的是包的真实路径,即/Users/novalagung/Documents/go/src/test-import/lib/

我应该怎么做?这可能吗?

英文:

I'm having difficulty when trying to get path of imported package. When I print result of os.Getwd() inside imported package, it's showing same path like on main package.

This what I did.

Project structure

How to get path of imported package

lib/lib.go

package lib

import "os"
import "fmt"

func init() {
    dir, _ := os.Getwd()
    fmt.Println("lib.init() :", dir)
}

func GetPath() {
    dir, _ := os.Getwd()
    fmt.Println("lib.GetPath() :", dir)
}

main.go

package main

import "os"
import "fmt"
import "test-import/lib"

func main() {
    dir, _ := os.Getwd()
    fmt.Println("main :", dir)
    lib.GetPath()
}

Result

lib.init() : /Users/novalagung/Documents/go/src/test-import
main : /Users/novalagung/Documents/go/src/test-import
lib.GetPath() : /Users/novalagung/Documents/go/src/test-import

The result of os.Getwd() from lib/lib.go is still same path like on main. What I want is the real path of the package which is /Users/novalagung/Documents/go/src/test-import/lib/

What should I do? Is it possible?

答案1

得分: 11

如果您可以获取包中某个东西的引用,您可以使用反射来获取导入路径。

这里有一个在Play上的示例:

package main

import (
	"bytes"
	"fmt"
	"reflect"
)

func main() {
	var b bytes.Buffer
	fmt.Println(reflect.TypeOf(b).PkgPath())
}
英文:

If you can get a reference to something in the package, you can use reflect to get the import path.

Here's an example on Play:

package main

import (
	"bytes"
	"fmt"
	"reflect"
)

func main() {
	var b bytes.Buffer
	fmt.Println(reflect.TypeOf(b).PkgPath())
}

答案2

得分: 2

PkgPath()只能获取非指针类型的包路径。

// 如果类型是预声明的(如string、error)或未定义的(*T、struct{}、[]int或A,其中A是未定义类型的别名),包路径将为空字符串。
func packageName(v interface{}) string {
	if v == nil {
		return ""
	}

	val := reflect.ValueOf(v)
	if val.Kind() == reflect.Ptr {
		return val.Elem().Type().PkgPath()
	}
	return val.Type().PkgPath()
}
英文:

PkgPath() only can retrieve the package path for non-pointer

// If the type was predeclared (string, error) or not defined (*T, struct{},
// []int, or A where A is an alias for a non-defined type), the package path
// will be the empty string.
func packageName(v interface{}) string {
	if v == nil {
		return ""
	}

	val := reflect.ValueOf(v)
	if val.Kind() == reflect.Ptr {
		return val.Elem().Type().PkgPath()
	}
	return val.Type().PkgPath()
}

huangapple
  • 本文由 发表于 2017年5月31日 12:01:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/44275403.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定