Go中的Println vs Printf vs Print [英] Println vs Printf vs Print in Go

查看:66
本文介绍了Go中的Println vs Printf vs Print的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我来自JS领域,经常使用console.logconsole.error

I come from a land of JS and have mostly used things like console.log or console.error

现在,我正在关注的教程中,那里的讲师做了类似的事情

Now, the tutorial I am following, the instructor over there did something like this

package main

import "fmt"



func main() {

    var FirstName = "Varun"
    var lastName = "bindal"

    fmt.Println(FirstName, lastName)
    fmt.Printf("%T", FirstName)
}

在这里,他做了PrintF而不是Println来检查类型.最初,我认为println在新行中打印,所以我更改了

Here he did PrintF to check type instead of Println. Initially, I thought that println prints in new Line so I changed my

fmt.Printf("%T", FirstName)

fmt.Println("%T", FirstName)

但此记录为%T Varun,而不是告诉我类型.

but this logged %T Varun instead of telling me the type.

我去了他们的网站来弄清楚,或者无法理解还是没有?无法找到它.

I went to their site to figure it out and was either unable to comprehend it or wasn't able to find it out.

谷歌搜索使我知道Go中有三种记录/打印方式

Googling lead me know that there are three ways to log/print in Go

  1. Println
  2. Printf
  3. 打印

那么,如果有人打电话告诉他们三个之间的区别?

So, If someone call tell the difference between three of them?

推荐答案

就像Nate所说的那样:fmt.Printfmt.Println打印原始字符串(fmt.Println追加换行符)

Just as Nate said: fmt.Print and fmt.Println print the raw string (fmt.Println appends a newline)

fmt.Printf不会打印新行,您必须使用\n将其添加到末尾.

fmt.Printf will not print a new line, you will have to add that to the end yourself with \n.

fmt.Printf的工作方式很简单,您提供一个包含某些符号的字符串,而其他参数替换这些符号.例如:

The way fmt.Printf works is simple, you supply a string that contains certain symbols, and the other arguments replace those symbols. For example:

fmt.Printf("%s is cool", "Bob") 

在这种情况下,%s代表字符串.就您而言,%T打印变量的类型.

In this case, %s represents a string. In your case, %T prints the type of a variable.

这篇关于Go中的Println vs Printf vs Print的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆