Hey all ππ½
This is a small series that I would be regularly updating as I have started learning Golang. This isnβt an actual tutorial or anything but rather a small collection of mistakes I made and things I overlooked.
Functions have to start with a capital letter if youβre looking to export them from modules
// my-published-module.go
package my_module
import "log"
func printMe() { // ππ½ This is wrong
log.Printf("Hello world v1")
}
func anotherPrint() { // ππ½ This is wrong
log.Printf("New world")
}
// app-where-i-am-consuming-module.go
package github.com/emilshr/stuff
import "github.com/emilshr/my-published-module"
func main() {
my_module.printMe() // ππ½ This errors out as undefined
// as the compiler is unable to find the function
}
I was pulling my hair trying to figure out a solution for this. When I published a sample module with the syntax mentioned above, I was unable to access the function and I was frustrated trying to figure out why.
However, I just came to know that in order to export a function, youβve got to start the function name with a capital letter. Yes, I feel so dumb right now π€¦π½ββοΈ
So the updated snippet would look like
// my-published-module.go
package my_module
import "log"
func PrintMe() { // β
This is correct
log.Printf("Hello world v1")
}
func AnotherPrint() { // β
This is correct
log.Printf("New world")
}
// app-where-i-am-consuming-module.go
package github.com/emilshr/stuff
import "github.com/emilshr/my-published-module"
func main() {
my_module.PrintMe() // β
This works
}