programming
Learning Golang as a Typescript dev
Date Published
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
1// my-published-module.go2package my_module34import "log"56func printMe() { // 👈🏽 This is wrong7 log.Printf("Hello world v1")8}910func anotherPrint() { // 👈🏽 This is wrong11 log.Printf("New world")12}1314// app-where-i-am-consuming-module.go15package github.com/emilshr/stuff1617import "github.com/emilshr/my-published-module"1819func main() {20 my_module.printMe() // 👈🏽 This errors out as undefined21 // as the compiler is unable to find the function22}
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. So the updated snippet would look like
1// my-published-module.go2package my_module34import "log"56func PrintMe() { // ✅ This is correct7 log.Printf("Hello world v1")8}910func AnotherPrint() { // ✅ This is correct11 log.Printf("New world")12}1314// app-where-i-am-consuming-module.go15package github.com/emilshr/stuff1617import "github.com/emilshr/my-published-module"1819func main() {20 my_module.PrintMe() // ✅ This works21}