Gofinity
solutions

Hello, Gofinity

Easy
← AllDdcodes
go.mod
module gofinity/hello

go 1.24
main.go
package main

import "fmt"

func Greet(name string) string {
	if name == "" {
		name = "World"
	}
	return fmt.Sprintf("Hello, %s!", name)
}

func main() {
	fmt.Println(Greet("Gofinity"))
}
main_test.go
package main

import "testing"

func TestGreet(t *testing.T) {
	tests := []struct {
		name  string
		input string
		want  string
	}{
		{name: "a name", input: "Gofinity", want: "Hello, Gofinity!"},
		{name: "another name", input: "Ada", want: "Hello, Ada!"},
		{name: "empty falls back to World", input: "", want: "Hello, World!"},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			if got := Greet(tt.input); got != tt.want {
				t.Errorf("Greet(%q) = %q, want %q", tt.input, got, tt.want)
			}
		})
	}
}
0 votes3 files42 lines