Golang 导入包

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

Golang import packages

问题

我在Golang和包含包的问题上遇到了一些问题。我有以下结构:

src/
├── hello_world
│   ├── hello.go
│   └── math
│       └── add.go

hello.go 文件包含以下代码:

package main

import (
    "fmt"
    math "hello_world/math"
)

func main() {
    fmt.Println("Hello World")
    x := math.add(6, 5)
}

还有 add.go 文件:

package math

func add(x, y int) int {
    return x + y
}

当我运行 go run hello.go 时,我看到以下错误:

evgen@laptop:~/go/src/hello_world$ go run hello.go 
# command-line-arguments
./hello.go:10: cannot refer to unexported name math.add
./hello.go:10: undefined: "hello_world/math".add

GOPATH

evgen@laptop:~/go/src/hello_world$ echo $GOPATH
/home/evgen/go

如何修复这个问题?谢谢!

英文:

I have some problem with Golang and include package. I have that scructure

src/
├── hello_world
│   ├── hello.go
│   └── math
│       └── add.go

hello.go file contains this code:

package main

import (
	"fmt"
	math "hello_world/math"
)

func main() {
	fmt.Println("Hello World")
	x := math.add(6, 5)
}

and add.go

package math

func add(x, y int) int {
    return x + y
}

and when I do go run hello go I see:

evgen@laptop:~/go/src/hello_world$ go run hello.go 
# command-line-arguments
./hello.go:10: cannot refer to unexported name math.add
./hello.go:10: undefined: "hello_world/math".add

GOPATH:

evgen@laptop:~/go/src/hello_world$ echo $GOPATH
/home/evgen/go

How fix it? Thanks you!

答案1

得分: 9

在包的外部,只能访问和引用导出的标识符,也就是以大写字母开头的标识符。

所以最简单的修复方法是将 math.go 中的 math.add() 函数改名为 Add() 并导出:

func Add(x, y int) int {
    return x + y
}

当你从 main.go 引用它时:

x := math.Add(6, 5)

另外需要注意的是,当导入 hello_world/math 包时,你不需要为其导出的标识符指定一个新名称:默认情况下,它将是导入路径的最后一部分,所以以下导入是等效的:

import (
    "fmt"
    "hello_world/math"
)
英文:

Outside of a package only the exported identifiers can be reached and referred to, that is identifiers that start with an uppercase letter.

So the easiest fix is to export your math.add() function by changing its name to Add() in math.go:

func Add(x, y int) int {
    return x + y
}

And, of course, when you refer to it from the main.go:

x := math.Add(6, 5)

And as a side note, note that when importing your hello_world/math package you don't have to specify a new name to refer to its exported identifiers: by default it will be the last part of its import path, so this is equivalent to your imports:

import (
    "fmt"
    "hello_world/math"
)

答案2

得分: 2

将你希望其他函数读取的函数在你的包中首字母大写:

func Add(x, y int) int {
    return x + y
}

然后在 hello.go 中这样调用它:

x := math.Add(6, 5)

保持它们小写确实有其目的,特别是如果你希望防止它在包外被意外使用。

英文:

Capitalize the function within your package that you want other functions to read:

func Add(x, y int) int {
    return x + y
}

then call it in hello.go like this:

x := math.Add(6, 5)

Keeping them lower case does have its purpose, particularly if you want to protect it from inadvertent use outside the package.

答案3

得分: 0

在你的主函数中调用Add函数时,不要使用这种方式:

x := math.Add(6 + 5)

而是使用这种方式:

x := math.Add(6, 5)
英文:

And when calling Add in your main function, dont use this

x := math.Add(6 + 5)

Instead use this

x := math.Add(6, 5)

答案4

得分: 0

函数、变量或来自不同包的任何内容都必须以大写字母开头,以便在导入到主包时可见。

示例:

package main

import "fmt"
import "other/out"

func main(){

fmt.Println(out.X)

// hello

}

<!-- -->

package other

var X string = "hi"
英文:

Functions, variables , anything coming from a different package have to start with a capital letter to make it visible for when importing into the main package.

example:

package main

import &quot;fmt&quot;
import &quot;other/out&quot;

func main(){

fmt.Println(out.X)

// hello

}

<!-- -->

package other

var X string = &quot;hi&quot;

huangapple
  • 本文由 发表于 2015年10月4日 17:55:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/32932199.html
匿名

发表评论

匿名网友

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

确定