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.go
2package my_module
3
4import "log"
5
6func printMe() { // 👈🏽 This is wrong
7 log.Printf("Hello world v1")
8}
9
10func anotherPrint() { // 👈🏽 This is wrong
11 log.Printf("New world")
12}
13
14// app-where-i-am-consuming-module.go
15package github.com/emilshr/stuff
16
17import "github.com/emilshr/my-published-module"
18
19func main() {
20 my_module.printMe() // 👈🏽 This errors out as undefined
21 // as the compiler is unable to find the function
22}


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.go
2package my_module
3
4import "log"
5
6func PrintMe() { // ✅ This is correct
7 log.Printf("Hello world v1")
8}
9
10func AnotherPrint() { // ✅ This is correct
11 log.Printf("New world")
12}
13
14// app-where-i-am-consuming-module.go
15package github.com/emilshr/stuff
16
17import "github.com/emilshr/my-published-module"
18
19func main() {
20 my_module.PrintMe() // ✅ This works
21}