gpt4 book ai didi

go - Golang,在安装后导入pkg

转载 作者:行者123 更新时间:2023-12-01 22:41:21 24 4
gpt4 key购买 nike

我是新手,我来自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中,我只是将它们推到python文件夹中的 lib/目录中。

这些是错误:
cannot refer to unexported name pyfuncs.pyRange
undefined: pyfuncs.pyRange

最佳答案

要导入的功能必须以大写字母开头。
一步步:

  • cd imports/pyfuncs
  • go install
  • 尝试访问pyfuncs时,请执行imoprt "imports/pyfuncs"
  • 通过pyfuncs.PyRange访问该函数
  • 关于go - Golang,在安装后导入pkg,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63320329/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com