Golang Journey – 2

Hi there! I keep learning golang. Today I wrote tests and created maps ­čÖé

First of all I created a function using Golang’s official documentation like below:

func Greetings(names []string) (map[string]string, error) {
	messages := make(map[string]string)

	for _, name := range names {

		message, err := Hello(name)

		if err != nil {
			return nil, err
		}

		messages[name] = message
	}

	return messages, nil

}

The above function expects a string map. It returns a string map and error. There is a loop in this function to iterate names.

If you pass an empty name to the Hello function, it will return an error.

func Hello(name string) (string, error) {
	if name == "" {
		return name, errors.New("The name can't be blank!")
	}

	message := fmt.Sprintf(randomFormat(), name)

	return message, nil
}

I also created a random function to return random hello values ­čÖé

// return a random string
func randomFormat() string {
	formats := []string{
		"Hi %v. How are you?",
		"Merhaba %v. Nas─▒ls─▒n?",
		"Hi. I'm %v. Nice to meet you",
	}

	return formats[rand.Intn(len(formats))]
}

I created a test file for this module. Every go test file should contains _test in their names.

package greetings

import (
	"regexp"
	"testing"
)

The header of this test file was like above. And I created my first test:

func TestHelloName(t *testing.T) {

	name := "Ali"

	want := regexp.MustCompile(`\b` + name + `\b`)

	message, err := Hello(name)

	if !want.MatchString(message) || err != nil {
		t.Fatalf(`Hello("Ali") = %q, %v, want match for %#q, nil`, message, err, want)
	}
}

The above example showed me how can I write a test. For example, your test function should starts with Test prefix. It should take a *testing.T. I think this an interface. I don’t know for now.

My second test was like that:

func TestHelloEmpty(t *testing.T) {

	message, err := Hello("")

	if message != "" || err == nil {
		t.Fatalf(`Hello("") = %q, %v want "", error`, message, err)
	}
}

All test should output Fatal. There are some format params in the string. Actually, I don’t know format params clearly for now. But, I test for empty string.

If you want to run these tests, just type this commands in your command line:

go test -v

=== RUN   TestHelloName
--- PASS: TestHelloName (0.00s)
=== RUN   TestHelloEmpty
--- PASS: TestHelloEmpty (0.00s)
PASS
ok      github.com/aligoren/go_learn/greetings  0.002s

That’s all. I imported my local file to my new module. Now, it should work as I expected.

main.go file should be like that:

package main

import (
	"fmt"
	"log"

	"github.com/aligoren/go_learn/greetings"
)

func main() {

	names := []string{"Ali", "Burak", "Can"}

	messages, err := greetings.Greetings(names)

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

	fmt.Println(messages)
}

I ran this command:

go run .

map[Ali:Hi Ali. How are you? Burak:Hi. I'm Burak. Nice to meet you Can:Hi Can. How are you?]

That’s all ­čÖé

You can check my GitHub repository for these two modules.

Thanks for reading ^_^