Gowhere

I started some Go code. "Go where?", I wondered. For some mysterious reasons, I liked the term Gowhere. Let’s create a repository on GitHub to play with Go (Golang)—Gowhere.

Heading to Golang official site, following the instruction, I started to write code in no time. It is really fast and easy to get started. Let’s start with the infamous "Hello World!" application.

Installing Go is easy and straightforward

  1. Download the installation package from the official website, choose the one for your OS. I installed for both MacOS and Windows
  2. Run the package and follow the instruction—Next and Next and Done
  3. Open the command line, run this command go version

I use VS Code, highly recommended, as my code editor. One I created my first Go file and opened with VS Code, it suggests all the plugins I need to be productive with Go. Nice!

So far, I’ve known Go in a single file—write code in a file, no executable package. To run a Go file, I ran go run filename.go in the terminal—either inside the VS Code or Command Line or PowerShell or MacOs terminal.

Create hello.go and try it out

package main

import (
    "fmt"
)

func main() {
    fmt.Println("Hello World! Greeting from Go")
}

Neat and straightforward. There are 3 parts—package, import, and func.

Package – define the namespace (in C#) or a higher logical abstraction for other functions, methods. Which means that every Go file requires a package definition.

import – import functions from other packages.

func – Define a function. In the above example, main is the entry point function.

What if I changed the names—package name to main1 or function name to main_v1? – Failed. The compose main_main is strictly required.

The bracket ({) must be in the same line with the function name. This code will cause compile error

func main() 
{
    fmt.Println("Hello World! Greeting from Go")
}

All those errors are caught at the compile time—Go is a statical language. With the help from VS Code, I do not have to remember or worry about them. I will remember them when I write more Go code.

Go has a set of data types—just like any other languages. What are new to me are the Defer and Pointer. I knew pointer back in the university. Since then I have not used them. Thank to the C#.

Defer allows developers to specify callback functions to be executed in Last In First Out (LIFO) order once the main function completes its execution. I’ve not understood what it is used for—maybe to clean up resources. But I think it is an important concept in Go. I will find out more when I step further.

"How do I pass the returned value from the host method to deferred methods?", I wondered. I figured one possible solution using pointer.

Pointer holds the address of a variable. Which allows the consumers access the latest value of a variable—I know your address, I will go there and get it. Sometimes, It is a dangerous thing, better use with care.

Defer functions are also functions. Therefore it is fine to write defer in defer as the below example.


func deferCallWorkLikeCallBack() {
    // Is it possible to pass the returned value from calling method to the deferred function?
    var number int = 1
    // This will pass 1 to the deferred method. So not a reference
    defer deferInDefer(number)
    // This will pass the latest number, which is 13, to the deferred function
    // Pass the address (pointer) of the number variable
    defer deferWithPointer(&number)
    fmt.Println("Hi I am a deferred call. You can think of me as a callback in your word")
    number = 13
}

func deferInDefer(number int) {
    fmt.Println("Hi you passed the number:", number)
}

func deferWithPointer(number *int) {
    // This allows the function to access the latest value passed from the calling function
    fmt.Println("Hi you passed the pointer to number:", *number)
}

If I pass a pointer to a deferred function, I should be able to access the latest value of a variable.

package main

import (
    "fmt"
)

func main() {
    defer deferCallWorkLikeCallBack()
    fmt.Println("Hello World! Greeting from Go")
}

Will produce this output

Hello World! Greeting from Go
Hi I am a deferred call. You can think of me as a callback in your word
Hi you passed the pointer to number: 13
Hi you passed the number: 1

That’s a good start. I might go somewhere.

Write a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.