gpt4 book ai didi

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

转载 作者:IT老高 更新时间:2023-10-28 12:59:54 29 4
gpt4 key购买 nike

我创建了一个名为 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 文件夹中。

最佳答案

你需要让你的函数可以用大写的名字导出:

func Fastget(...

用作:

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

规范提到:“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 - 在 Go Lang 中导入自定义包时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25501875/

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