duancashi1362 2018-12-21 04:27
浏览 71
已采纳

Go中的Println vs Printf vs Print

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)
}

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)

to

fmt.Println("%T", FirstName)

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.

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

  1. Println
  2. Printf
  3. Print

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

  • 写回答

3条回答 默认 最新

  • doz15449 2018-12-21 04:51
    关注

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

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

    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") 
    

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度