在Go语言中导入自定义包时出现错误。

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

Error in importing custom packages in Go Lang

问题

我已经创建了一个名为libfastget的库,它位于我的程序的src目录下,结构如下:

src
|-libfastget
|  |-libfastget.go
|
|-MainProgram
   |-main.go

libfastget导出了一个名为fastget的函数,代码如下:

package libfastget

import (
	"fmt"
	"io"
)

func fastget(urlPtr *string, nPtr *int, outFilePtr *string) download {
	.....
	return dl
}

当我在主程序中使用这个库时,代码如下:

package main

import (
	"fmt"
	"net/http"
	"os"
	"libfastget"
	"path/filepath"
	"strings"
	"flag"
	"time"
)

func uploadFunc(w http.ResponseWriter, r *http.Request) {
    n := libfastget.fastget(url, 4, filename)
}

在尝试使用go build构建时,我遇到了以下错误:

# FServe
./main.go:94: cannot refer to unexported name libfastget.fastget
./main.go:94: undefined: libfastget.fastget

奇怪的是,库文件libfastget.a确实存在于pkg文件夹中。

英文:

I have created a library by the name libfastget which is in the src with my program as

src
|-libfastget
|  |-libfastget.go
|
|-MainProgram
   |-main.go

and the libfastget exports a funtion fastget as follows

package libfastget

import (
	"fmt"
	"io"

)


func fastget(urlPtr *string, nPtr *int, outFilePtr *string) download {
	.....
	return dl

}

When I use the library in my main program

package main

import (
	"fmt"
	"net/http"
	"os"
    "libfastget"
	"path/filepath"
	"strings"
	"flag"
	"time"

)
func uploadFunc(w http.ResponseWriter, r *http.Request) {

         n:=libfastget.fastget(url,4,filename)

	}

}

I get the following error upon trying to build with go build

# FServe
./main.go:94: cannot refer to unexported name libfastget.fastget
./main.go:94: undefined: libfastget.fastget

The strange thing is that the library file libfastget.a is present in the pkg folder.

答案1

得分: 142

你需要将函数的名称大写以使其可导出:

func Fastget(...

用法如下:

n := libfastget.Fastget(url, 4, filename)

规范中提到:“导出标识符”:

为了允许从另一个包中访问标识符,可以将其导出。如果满足以下两个条件,则标识符被导出:

  • 标识符名称的第一个字符是一个 Unicode 大写字母(Unicode 类别“Lu”);
  • 标识符在包块中声明,或者它是字段名称方法名称

所有其他标识符都不会被导出。

英文:

you would need to make your function exportable with an uppercase for its name:

func Fastget(...

Used as:

n:=libfastget.Fastget(url,4,filename)

The spec mentions: "Exported identifiers":

> An identifier may be exported to permit access to it from another package. An identifier is exported if both:
>
>- the first character of the identifier's name is a Unicode upper case letter (Unicode class "Lu"); and
>- the identifier is declared in the package block or it is a field name or method name.
>
> All other identifiers are not exported.

答案2

得分: 3

将一个函数导出到另一个包中,函数的标识符必须以大写字母开头。

英文:

to export a function into another package the function identifier must start with a capital letter.

答案3

得分: 0

我最近开始学习GO语言(两天前)。
我发现你需要设置一个工作区文件夹,以便将本地包导入到其他项目或main.go文件中。我正在使用VS Code编辑器。如果我错了,请纠正我,但是这个设置对我来说很有效。

在你的bash_profile.zshrc文件中添加以下行,根据你的文件夹路径更新GOPATH。

export GOPATH=~/projects/GO_PROJECTS
export PATH=$PATH:$GOPATH/bin:$PATH

在Go语言中导入自定义包时出现错误。

这是我的sayHello.go文件,请注意,为了能够导出一个函数,func的名称应该以大写字母开头,比如SayHello

package utils

import "fmt"

func SayHello() {
    fmt.Println("Hello, Ajinkya")
}

现在我可以将utils包导入到main.go文件中。

package main

import (
    "go_proj1/utils"
)

func main() {
    utils.SayHello()
}
英文:

I recently started learning GO Lang (2 days back)
And what I found was you need to setup a workspace folder to make the local packages import into other projects or main.go files. I'm using VS Code editor. Please correct me if Im wrong, but this setup works fine for me.

Inside your bash_profile OR .zshrc file add below lines, update the GOPATH as per your folder path.

export GOPATH=~/projects/GO_PROJECTS
export PATH=$PATH:$GOPATH/bin:$PATH

在Go语言中导入自定义包时出现错误。

and this is my sayHello.go file, please note to be able to export a function the func name should start with a CapitalCase SayHello

package utils

import "fmt"

func SayHello() {
	fmt.Println("Hello, Ajinkya")
}

and now I am able to import utils package into main.go file

package main

import (
	"go_proj1/utils"
)

func main() {
	utils.SayHello()
}

答案4

得分: -1

以下是翻译好的内容:

  1. 将当前目录设置为GOPATH。

  2. 或者你可以使用本地导入,如下所示:

    将你的main.go文件移动到../目录下的libfastget.go文件旁边。
    我的意思是文件结构看起来像这样:
    src
    |-libfastget
    | |-libfastget.go
    |
    |-main.go

    导入语句:"./libfastget"

英文:
  1. set the current directory as GOPATH
  2. or you can use local import as follows
    >
    move your main.go to the ../ directory to the libfastget.go.
    i mean the files looks like:
    src
    |-libfastget
    | |-libfastget.go
    |
    |-main.go

import "./libfastget"
>

huangapple
  • 本文由 发表于 2014年8月26日 17:01:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/25501875.html
匿名

发表评论

匿名网友

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

确定