在Go Lang中导入自定义软件包时出错 [英] Error in importing custom packages in Go Lang

查看:183
本文介绍了在Go Lang中导入自定义软件包时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在 src 中创建了一个名为 libfastget 的库,其中我的程序为

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

libfastget 导出一个函数 fastget 如下

 包libfastget 

导入(
fmt
io




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

}


$ b

当我在我的主程序中使用库时

  


$ f








$ path / filepath
strings
flag
time

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

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

}

}

在尝试使用构建

时出现以下错误:

 #FServe 
./main .go:94:无法引用未导出的名称libfastget.fastget
./main.go:94:undefined:libfastget.fastget

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

解决方案

<你可能需要使用大写字母来输出你的函数的名字:

  func Fastget(... 

用作:

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

该规格提到: 导出的标识符


可以导出标识符以允许从另一个软件包访问它。如果同时输出标识符:




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

    $ b


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



    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.

    解决方案

    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.

    这篇关于在Go Lang中导入自定义软件包时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆