Now that we've covered a basic understanding of Go, it's time to write some code!
Every programmer's journey begins with the classic "Hello, World!" program. This simple exercise will help you understand the basics of printing output in Go.
In Go, we use the fmt package (short for "format") to handle input and output operations. Before you can use any package in Go, you need to import it at the top of your file.
import "fmt"
Go provides several functions in the fmt package for printing output:
// Prints text without a newline
fmt.Print("Hello")
// Prints text with a newline at the end
fmt.Println("Hello, World!")
// Prints formatted text
fmt.Printf("Hello, %s!\n", "World")
The most commonly used function is fmt.Println() (print line), which automatically adds a newline character at the end of your output.
fmt.Println("Hello, World!")
// Output: Hello, World!
// (cursor moves to next line)
fmt.Print(
A complete Go program looks like this:
package main
import "fmt"
func main() {
fmt.Println("Hello, World!")
}
Every Go program starts execution in the main() function within the main package.
To begin, we're gonna start easy. Let's see if you can print out "Hello, World!" to the console.
In the code editor to the right, add the necessary code to print out "Hello, World!" followed by a new line. Once you've made those changes, press the run button to see if your code works and if it passes the test.