Use the fmt.Println function to print output to the console
Use the fmt.Println function to print output to the console
In the Go language, the fmt package provides a series of functions for formatting input and output. Among them, the fmt.Println function is one of the commonly used functions, through which we can print the content to the console.
Below, I will give some examples of using the fmt.Println function to help readers better understand its usage and functions.
- Print string
First, we can use the fmt.Println function to directly print a string. For example, if we want to print "Hello, World!", we can use the following code:
package main import "fmt" func main() { fmt.Println("Hello, World!") }
Execute the above code, the console will output:
Hello, World!
- Print variables
In addition to printing fixed strings, we can also print the values of variables. For example, we define an integer variable x and assign it a value of 10. We can then print out the value of variable x using the fmt.Println function.
package main import "fmt" func main() { x := 10 fmt.Println("x的值是:", x) }
Execute the above code, the console will output:
x的值是: 10
- Printing multiple variables or strings
fmt.Println function also supports simultaneous printing Multiple variables or strings will be separated by spaces. For example, we define two string variables name and age and assign them corresponding values. Then, use the fmt.Println function to print the values of these two variables.
package main import "fmt" func main() { name := "Alice" age := 20 fmt.Println("姓名:", name, "年龄:", age) }
Execute the above code, the console will output:
姓名: Alice 年龄: 20
- Formatted output
fmt.Println function also supports formatted output. For example, we can use the following code to print the value of the numeric variable pi to two decimal places:
package main import "fmt" func main() { pi := 3.14159 fmt.Printf("π的值保留两位小数:%.2f ", pi) }
Execute the above code, the console will output:
π的值保留两位小数:3.14
- Newline and not Line wrap
Finally, we can also control whether the fmt.Println function wraps the line after output. By default, this function adds a newline character to the output. If we want to output without line breaks, we can use the fmt.Print function. The following is an example:
package main import "fmt" func main() { fmt.Print("Hello, ") fmt.Println("World!") // 换行 fmt.Print("Hello, ") fmt.Print("World!") // 不换行 }
Execute the above code, the console will output:
Hello, World! Hello, World!
Through the above examples, we can see that the fmt.Println function prints output to the control in the Go language The role of the platform. Whether printing strings, variables, or formatting output, fmt.Println is a very convenient function.
I hope this article will help you understand and use the fmt.Println function. Enjoy the fun of Go programming!
The above is the detailed content of Use the fmt.Println function to print output to the console. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Installing the Group Policy Management Console (also known as GPMC) on Windows 11 will be the topic of today’s post. In Windows systems, the tools we are discussing improve the management of Group Policy by making it easier for IT and system administrators to understand. Be careful not to confuse the Local Group Policy Editor (gpedit.msc) with the Group Policy Management Console (GPMC). In terms of local system settings, Gpedit works with the registry, but GPMC works with server management settings for domain-based networks. You need to download and install the Windows Remote Server Administration Tools, sometimes called RSAT, to accomplish this. Use remote server management

Have a Windows 11 touchscreen laptop that won't calibrate or doesn't work? This can be frustrating, especially if the only access option is through a device with a touch screen. While Windows touch screen devices are known for their smooth functionality, especially for graphically demanding applications, things can sometimes go wrong. You may encounter issues such as the touch screen not working properly or sometimes the Windows 11 touch screen won't calibrate at all. While we have already covered how to calibrate your touch screen on Windows 10, here we will discuss some solutions that may help you when your Windows 11 touch screen won’t calibrate. Does the touch screen work with Wind?
![Xbox System Error E200 [Fixed]](https://img.php.cn/upload/article/000/465/014/170832475129577.jpg?x-oss-process=image/resize,m_fill,h_207,w_330)
This article will explain how to resolve system error E200 on your Xbox console. Typically, this error occurs when your Xbox console gets interrupted while trying to install the latest console operating system update. This error may also occur if the system update is interrupted due to a power outage or network problem. Fix Xbox System Error E200 Use the following fix to fix System Error E200 on your Xbox console: Turn your Xbox console off and on again Perform a system update factory reset your console Let’s get started. 1] Turning your Xbox console off and on again Resetting the power cycle of your Xbox console can effectively eliminate potential temporary glitches and resolve some issues. Follow these steps to turn off and reopen Xbox Control

The Xbox gaming console is a favorite among gamers. With the new SeriesX and SeriesS, gaming is almost a lifelike experience. The Xbox controller is your primary tool for experiencing gaming effects. Sometimes the controller connection gets cut off or some errors are encountered while trying to connect the controller to the main console. This may be due to various issues related to pairing. This can be overcome with a few simple steps. Reset your Xbox Series S or Xbox Series X controller Step 1: Press and hold the Xbox button on your controller for a few seconds to turn off the controller. Step 2: On the screen, go to Turn off controller and press button A to select that option. NOTE: If you keep pressing X

Microsoft Store is one of the most important applications installed for Windows 11 users. Here you can purchase apps, games, and other content for your computer. With the launch of Windows 11, these programs also come with updated UI to match the operating system design, and Microsoft Store is not far behind. Its main purpose is to provide a unified experience for finding and downloading software and applications. You can browse categories like games, music, movies, TV shows, and more. The newly revamped store offers more services than the Windows 10 version. One of the most notable differences between the two stores is their design. Secondly, there are more applications and games, not only

There are many features and services running on Windows PC, allowing users to take full advantage of it. They are distributed throughout the system and often not accessible in one location. Therefore, the need for Windows 11 computer management is crucial as it allows you to access a range of Windows management tools to manage your computer. What is Computer Management Console? Computer Management is a set of management tools on Windows PCs that Microsoft provides for managing local and remote computers. It allows users to access many management tools such as Task Scheduler, Event Viewer, Device Manager, Disk Management, Service Manager, etc. Additionally, every management tool in console management is encapsulated and organized into a single console

There are several ways to clear the console or output screen, one of which is the clrscr() function. It clears the screen when the function is called. It is declared in the "conio.h" header file. There are some other methods such as system("cls") and system("clear"), which are declared in the "stdlib.h" header file. The following is the syntax for clearing the console in C language: clrscr();ORsystem("cls");ORsystem("clear");The following is a use

How to use the print() function in Python3.x to output content to the console. In Python, the print() function is a very commonly used function, which can output specified content to the console. This article will introduce in detail how to use the print() function to achieve this function, and attach the corresponding code examples. First, let us understand the basic usage of the print() function. The print() function can accept one or more parameters as input and put them in comma-separated form
