duanqian6295 2015-12-20 19:00
浏览 40
已采纳

转到:从库调用函数

Now I'm really confused. Here is my problem (Go is new to me):

Firs file:

// main.go
package main

import "./libraries/test"

func main() {

    test.foo()
}

Second file:

// test.go
package test

import "fmt"

func foo() {

    fmt.Println("foo")
}

My structure looks like this:

main.go
   /libraries
        /test
           test.go

If I compile this code I'll get this error messages:

./main.go:7: cannot refer to unexported name test.foo

./main.go:7: undefined: test.foo

If I change foo to Foo everywhere the error is gone and the program works as expected.

  • 写回答

2条回答 默认 最新

  • dongshi4078 2015-12-20 19:06
    关注

    In Go, it's an important distinction whether a symbol's name is written in upper or lower camel case. This goes for functions, but also for types (like structs or interfaces) and also struct members.

    You can read this up in the Go documentation (emphasis mine):

    Names are as important in Go as in any other language. They even have semantic effect: the visibility of a name outside a package is determined by whether its first character is upper case.

    This means that you cannot name functions and types arbitrarily. If you need to call this function from another module, it must be named Foo, and not foo.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 python怎么在已有视频文件后添加新帧
  • ¥20 虚幻UE引擎如何让多个同一个蓝图的NPC执行一样的动画,
  • ¥15 fluent里模拟降膜反应的UDF编写
  • ¥15 MYSQL 多表拼接link
  • ¥15 关于某款2.13寸墨水屏的问题
  • ¥15 obsidian的中文层级自动编号
  • ¥15 同一个网口一个电脑连接有网,另一个电脑连接没网
  • ¥15 神经网络模型一直不能上GPU
  • ¥15 pyqt怎么把滑块和输入框相互绑定,求解决!
  • ¥20 wpf datagrid单元闪烁效果失灵