Skip to content

Go ​

Version ​

Running Go 1.23

Supported languages ​

Go

Testing framework ​

Go testing (built-in)

Special reminders and implementation details ​

go
package main

import "testing"

func TestHelloWorld(t *testing.T) {
	t.Run("It should return 'Hello World!'", func(t *testing.T) {
		result := HelloWorld()
		if result != "Hello World!" {
			t.Errorf("Expected 'Hello World!' but got '%s'", result)
		}
	})
}

Included libraries ​

  • Go standard library v1.23 (including testing, fmt, and other built-in packages)
  • External packages can be added via the dependency manager pkg.go.dev

Reading input (stdin) ​

Programs that read values use standard input. Run opens an interactive terminal: the program starts, prints its prompt, and you type the answer and press Enter, as in a real terminal. The tests of an output-test challenge feed the input themselves.

go
package main

import (
	"bufio"
	"fmt"
	"os"
	"strings"
)

func main() {
	reader := bufio.NewReader(os.Stdin)
	name, _ := reader.ReadString('\n')
	name = strings.TrimSpace(name)
	fmt.Printf("Hello, %s!\n", name)
}

ReadString returns the line including its newline and an io.EOF error at end-of-file (the text read so far is still returned).

How to debug ​

Debugging Go code is essential for every developer. One of the simplest yet effective ways to debug is by using fmt.Println and fmt.Printf statements.

Steps to Debug Using fmt.Println and fmt.Printf Statements ​

  1. Identify the Problem Area: Locate the section of your code where you suspect the problem might be.

  2. Insert fmt.Println or fmt.Printf Statements: Add fmt.Println or fmt.Printf statements before and after the suspected problematic code to print out variable values and flow control.

  3. Check the Results Output of your tests.

  4. Analyze the Output: Look at the values printed on the results console to understand what's going wrong in your code. Adjust your code based on the findings and repeat as necessary.

Here's an example of how to use fmt.Println for debugging:

go
package main

import "fmt"

func CalculateSum(a, b int) int {
	fmt.Printf("CalculateSum called with arguments: %d, %d\n", a, b) // Log function call
	sum := a + b
	fmt.Printf("Sum after addition: %d\n", sum) // Log sum calculation
	return sum
}