How to Print in Golang

Hey! If you love Go and building Go apps as much as I do, let's connect on Twitter or LinkedIn. I talk about this stuff all the time!

Want to learn how to build better Go applications faster and easier? You can.

Check out my course on the Go Standard Library. You can check it out now for free.


If you’re looking to learn how to print in Golang, you’re in the right place. Before diving into the code examples, let’s first understand the theory behind printing in Golang.

Printing in Golang is done using the fmt package, which provides various functions for formatting and printing text. The fmt package is part of the standard library in Golang and is widely used for printing messages to the console, generating logs, and formatting data for output.

The fmt package provides three main functions for printing text: Print, Printf, and Println. Here’s how they work:

Print: This function is used to print text to the console. It doesn’t provide any formatting options and simply prints the text as-is. Here’s an

example:

package main

import "fmt"

func main() {
    fmt.Print("Hello, world!")
}

In the code above, we use the fmt.Print() function to print the text “Hello, world!” to the console.

Printf: This function is used to format and print text to the console. It provides a variety of formatting options for different types of data, such as strings, numbers, and dates. Here’s an example:

package main

import "fmt"

func main() {
    name := "Alice"
    age := 30
    fmt.Printf("My name is %s and I'm %d years old.", name, age)
}

In the code above, we use the fmt.Printf() function to format and print a string with variables. The %s and %d are format specifiers that indicate the type of data to be printed (string and integer, respectively). The variables “name” and “age” are substituted for the format specifiers using the comma-separated list of arguments.

Println: This function is used to print text to the console, similar to the Print function. The difference is that Println automatically adds a newline character at the end of the output.

Here’s an example:

package main

import "fmt"

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

In the code above, we use the fmt.Println() function to print two lines of text, with each line on a separate line.

So why would someone want to print in Golang? One common use case is for debugging purposes. By printing messages to the console, you can check the values of variables and the flow of your program to identify any errors or issues.

In conclusion, printing in Golang is a fundamental operation that you’ll encounter frequently in your Golang programming journey. By using the fmt package and its various functions, you can easily print text to the console and format it for different types of data.

I hope you found this tutorial helpful. If you have any questions or comments, please feel free to leave them below.


Questions or comments? Reach out to me


Learn how to leverage the Go Standard Library like a PRO.

I just created a new course, The Go Standard Library, check it out!