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.
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
.
I suppose that you have not read the Go docs very closely. All names which begin with capital letters are exported from their package. All names in lowercase are not exported.