Skip to content

Learning Golang as a Typescript dev

Updated: at 08:48 AM

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
}