首页 > 解决方案 > Golang,安装后导入 pkg

问题描述

我很新,我来自python。为了学习,我正在重新创建 python 内置函数。
我被困在我的第一个功能上——范围,它还不是生成器。
我希望能够:

// testmain.go

package main

import (
    "pyfuncs"
    "fmt"
)

func main(){
    fmt.Println( pyfuncs.range(12) )
    fmt.Println( pyfuncs.range(-12, -2, 3) )
}

输出:

[1 2 3 4 5 6 7 8 9 10 11 12]
[-12 -9 -6 -3]
*/

$GOPATH/的结构是这样的:

/$GOPATH/
        |
        | src/
        |    | imports/
        |    |        | pyfuncs/
        |    |        |        | pyrange.go (package pyfuncs)
        |    | learn/
        |    |      | testmain.go (package main) <- This doesnt work
        |    |      | ArraysAndSlices/...
        |    |      | Printf/...
        |
        | pkg/
        |    | linux_amd64/
        |    |            | imports/
        |    |            |        | pyfuncs.a
        | bin/...

我在 testmain.go 如何从 pyfuncs 目录访问函数?
我希望能够从 fmt 等未来的每个项目中访问它们。在 python 中,我只是将它们推到lib/python 文件夹中的目录中。

这些是错误:

cannot refer to unexported name pyfuncs.pyRange
undefined: pyfuncs.pyRange

标签: goimport

解决方案


要导入的函数必须以大写字母开头。

一步步:

  1. cd imports/pyfuncs
  2. go install
  3. 当尝试访问 pyfuncs 时imoprt "imports/pyfuncs"
  4. 通过以下方式访问该功能pyfuncs.PyRange

推荐阅读