Golang Journey – 1

Hi there 🙂 Today I wrote a post about starting to learn Golang. I have already installed the golang.

Golang Gopher

Hello World

To write hello world, you first create a module following these ways;

mkdir hello
cd hello

I created a directory named hello and changed my current directory as hello.

go mod init hello

I created a module named hello. It also creates a file called go.mod. After, I created a file called hello.go

package main

import "fmt"

func main() {
  fmt.Println("Hello world")
}

Installing External Modules

If you want to install external modules, you can use go get command.

go get example.com/modulename

Now you can use the module you’ve installed.

Using Local Modules

For example, you created a module and you want to use it your main project. For example you have a project called ataturk.

go mod init github.com/aligoren/ataturk

I also have a project named hello 🙂 To use this local project, you should edit your main project’s go.mod file.

replace github.com/aligoren/ataturk => ../ataturk

Now, you should build your project using go build command. It will also add a required package like below;

require github.com/aligoren/ataturk v0.0.0-00010101000000-000000000000

If you want to use this module, you should call it like below;

package main

import (
	"fmt"
	"github.com/aligoren/ataturk"
)

func main() {
	fmt.Println(ataturk.Quote())
}

Running Go Project

If you don’t want to build your project but you want to run, you should use the below command;

go run .

We should see something like that

Benim naciz bedenim elbet bir gün toprak olacaktır

Mustafa Kemal ATATÜRK

Error Handling

Golang’s error handling mechanism is different than others. Let me explain you.

If you create a function which returns two values, you must handle these two.

If your function’s second value is a nil value, your function should be worked as you expected. But, if you return a value different than nil as a second value. Let me create a function to explain this.

func Hello(name string) (string, error) {

	if name == "" {
		return "", errors.New("Empty name!")
	}

	message := fmt.Sprintf("Hi, %v. How are you?", name)

	return message, nil
}

In the above example, our function expects a parameter to show a name. What would happen if the user didn’t pass the name.

If the name is equals empty string, the function will return two values. First value will empty, and the second value will be error.

We will use this method like below;

package main

import (
	"errors"
	"fmt"
	"log"
)

func Hello(name string) (string, error) {

	if name == "" {
		return "", errors.New("Empty name!")
	}

	message := fmt.Sprintf("Hi, %v. How are you?", name)

	return message, nil
}

func main() {
	log.SetPrefix("Error Handling: ")
	log.SetFlags(0)

	message, err := Hello("")

	if err != nil {
		log.Fatal(err)
	}

	fmt.Println(message)
}

If you run this method, it will check whether err value is nil or not. If err different than error, it also means we should show an error to the user. If it’s a nil value, it means the user passed a valid value 🙂 We’ll see the below output if you run above program

If you run this method, it will check whether err value is nil or not.

That’s all for now.

I created a GitHub repository for my Golang works. You can check it.

https://github.com/aligoren/go_learn

Btw, I found my first Golang project on GitHub. It was a file downloader. When I create that project, there wasn’t a command to create modules. Everything was a little hard. My first commit sent on May 29, 2015

https://github.com/aligoren/gofret/commits/master