CodeGym /행동 /Go SELF /Go에서 화면에 출력하기: fmt.Println and fmt.Print

Go에서 화면에 출력하기: fmt.Println and fmt.Print

Go SELF
레벨 1, 레슨 2
사용 가능

1. Getting to know fmt.Println

You have just written your first Go program and displayed your first message on the screen. Congratulations on that. Now let us keep going...

Here are 2 facts about commands in Go:

Any program in Go consists of instructions (commands). Commands come in different forms. The Go language has commands for every situation. Each command describes some specific action.

In Go, a semicolon at the end of a line is usually not needed: a newline is enough.

Examples of output commands:

Command Description (what it does)
fmt.Println(1)
Outputs a number to the screen:
1
fmt.Println("Amigo")
Outputs a string to the screen:
Amigo
fmt.Println("Risha & Amigo")
Outputs a string to the screen:
Risha & Amigo

In fact, this is one command — fmt.Println. And inside the parentheses you pass parameters to it. Depending on the parameter values, one command can perform different actions. This is very convenient.

Letter case matters

In Go, case matters: it is important whether commands are written with uppercase or lowercase letters. The command fmt.Println() will work, but fmt.println() will not. If you write the command with the wrong letters, Go will not recognize it and therefore will not execute it. So be careful.

Double quotes

If you want to print text to the screen, you need to enclose it on both sides with double quotes.

A single quote looks like this ', and a double quote looks like this ". A double quote is not two single quotes: please do not confuse them.

In Go, single quotes are usually used not for strings, but for one character (rune), for example: 'A'.

Double is the key next to Enter. It usually also has the Cyrillic letter "E" on it.

2. Getting to know fmt.Print

There is another command similar to fmt.Println(), and it is called fmt.Print().

The command fmt.Println() prints text and then adds a cursor move to the next line, as if pressing Enter. Thus, the next fmt.Println() command will print text on the next line. And the following one — on the next one, and so on.

Unlike fmt.Println(), the command fmt.Print() does not add Enter after printing text. Therefore, text printed by several fmt.Print() commands will be displayed on one line.

Example 1:

package main

import "fmt"

func main() {
	fmt.Print("Amigo")
	fmt.Println("The")
	fmt.Print("Best")
}
Mixed output with Print and Println

It will output:

AmigoThe
Best

Example 2:

package main

import "fmt"

func main() {
	fmt.Print("Amigo")
	fmt.Print("The")
	fmt.Print("Best")
}
Only Print — everything on one line

It will output:

AmigoTheBest

If you want to print text on one line, use Print; if you want several lines, use Println. It is all simple.

3. Understanding fmt.Println

If you know even a little English, learning programming will be much easier for you. The names of many commands in Go are directly related to what they do.

For example, Println is print + ln — print a line. I think everyone knows the word print, and ln is short for line. Each line of text on the screen is a separate line. So to print a line means exactly to output a line of text (and move to a new line).

fmt is a package in the Go standard library. Its name is usually decoded as format (formatting). It is most often used to print text to the screen (in the console). A console is the old name for a device that allowed commands to be entered from the keyboard and results to be displayed on the screen.

In other words, working with the console means outputting text to the screen and entering data using the keyboard. So if in the future you hear that a program printed something to the console, it simply means that it displayed text on the screen.

So it turns out that fmt.Println is roughly "Hey, console, print a line of text on the screen for me (and move to the next one)".

4. Examples of outputting different data

Let us play with console output. We will start with the simplest case and gradually make it more complex:

Outputting numbers

package main

import "fmt"

func main() {
	fmt.Println(1001)
	fmt.Println(-15)
}
Outputting numbers

Outputting text

package main

import "fmt"

func main() {
	fmt.Println("Programming is awesome!")
	fmt.Println("We joked, and now we code.")
}
Outputting text

Outputting several values at once

If you want to output several values, the easiest way is to pass several arguments to fmt.Println:

package main

import "fmt"

func main() {
	fmt.Println("My age:", 28)
}
Several arguments in fmt.Println

It will output:

My age: 28

This makes it very convenient to build meaningful sentences:

package main

import "fmt"

func main() {
	fmt.Println("It is", 2026, "now.")
}
Combining meaning without concatenation: several arguments

It will output:

It is 2026 now.

5. Common mistakes when using fmt.Println

Young Jedi often step on the same rakes. But so that you do not turn into the hero of the book "1001 errors in Go", here is a list of common mistakes and tips on how to avoid them.

Mistake 1. Forgot to close the string — lost the second quote.

If you write it like this (note: the string is not closed):

package main

import "fmt"

func main() {
	fmt.Println("Oops, opened a quote and forgot to close it);
}

The compiler will complain with something like "string literal not terminated" — close your strings!

Mistake 2. Forgot to import the fmt package

In Go, to use fmt.Println, you need to import the fmt package.

package main

func main() {
	fmt.Println("If there is no import fmt, there will be a compilation error")
}

Mistake 3. Using the wrong quotes

Beginners sometimes use angled quotes or single quotes:

package main

import "fmt"

func main() {
	fmt.Println(‘Error’) // incorrect: these are not standard double quotes
	fmt.Println('Error') // incorrect: in Go, single quotes are for one character (rune)
	fmt.Println("Error") // correct
}

For strings, always use standard double quotes — like this: ".

1
과제
Go SELF, 레벨 1, 레슨 2
잠금
첫 번째 인사
첫 번째 인사
1
과제
Go SELF, 레벨 1, 레슨 2
잠금
두 줄
두 줄
1
과제
Go SELF, 레벨 1, 레슨 2
잠금
사실 출력
사실 출력
1
과제
Go SELF, 레벨 1, 레슨 2
잠금
화살표 경로
화살표 경로
코멘트
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION