在Go Lang中导入自定义包时出错

She*_*lva 36 go

我创建了由名称的库libfastget这是在src我的程序作为

src
|-libfastget
|  |-libfastget.go
|
|-MainProgram
   |-main.go
Run Code Online (Sandbox Code Playgroud)

libfastget输出fastget如下功能

package libfastget

import (
    "fmt"
    "io"

)


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

}
Run Code Online (Sandbox Code Playgroud)

当我在主程序中使用库时

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)

    }

}
Run Code Online (Sandbox Code Playgroud)

尝试构建时出现以下错误 go build

# FServe
./main.go:94: cannot refer to unexported name libfastget.fastget
./main.go:94: undefined: libfastget.fastget
Run Code Online (Sandbox Code Playgroud)

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

Von*_*onC 91

你需要使用大写的名称输出你的函数:

func Fastget(...
Run Code Online (Sandbox Code Playgroud)

用作:

n:=libfastget.Fastget(url,4,filename)
Run Code Online (Sandbox Code Playgroud)

规范提到:" 导出的标识符 ":

可以导出标识符以允许从另一个包访问它.如果两者都导出标识符:

不导出所有其他标识符.