您当前的位置:首页 > 科技知识

go语言如何打印符号及其用法揭秘

作者:远客网络

go璇█杈撳嚭绗﹀彿鏄粈涔? /></p>
<p><p>Go璇█鐨勮緭鍑虹鍙蜂富瑕佸寘鎷?strong>fmt鍖呬腑鐨凱rint銆丳rintln鍜孭rintf鍑芥暟</strong>銆備互涓嬫槸瀵硅繖浜涘嚱鏁扮殑璇︾粏瑙i噴锛?/p>
</p>
<p><p>1銆?strong>fmt.Print</strong></p>
<p>2銆?strong>fmt.Println</strong></p>
<p>3銆?strong>fmt.Printf</strong></p>
</p>
<p><p><strong>fmt.Println</strong> 鏄疓o璇█涓渶甯哥敤鐨勮緭鍑哄嚱鏁颁箣涓€銆傚畠涓嶄粎鍙互杈撳嚭瀛楃涓层€佸彉閲忕瓑鍐呭锛岃€屼笖浼氬湪杈撳嚭鍐呭鐨勬湯灏捐嚜鍔ㄦ坊鍔犱竴涓崲琛岀銆備緥濡傦細</p>
</p>
<p><pre><code class=package main

import "fmt"

func main() {

fmt.Println("Hello, World!")

}

杩欐浠g爜灏嗚緭鍑?quot;Hello, World!"锛屽苟鍦ㄥ叾鍚庤嚜鍔ㄦ崲琛屻€?/p>

涓€銆丗MT.PACKAGE浠嬬粛

fmt 鍖呮槸Go璇█鏍囧噯搴撲腑鐨勪竴涓寘锛岀敤浜庢牸寮忓寲I/O銆傝繖涓寘鎻愪緵浜嗕竴浜涘嚱鏁帮紝鐢ㄦ潵鏍煎紡鍖栧瓧绗︿覆骞跺皢鍏惰緭鍑哄埌鏍囧噯杈撳嚭銆佹枃浠舵垨鑰呭叾浠栬緭鍑烘祦銆備互涓嬫槸fmt鍖呬腑甯哥敤鐨勫嚱鏁帮細

  • Print锛氱洿鎺ヨ緭鍑哄唴瀹癸紝涓嶅甫浠讳綍鏍煎紡鍖栥€?/li>
  • Println锛氳緭鍑哄唴瀹瑰苟鍦ㄦ湯灏炬坊鍔犳崲琛岀銆?/li>
  • Printf锛氭牸寮忓寲杈撳嚭鍐呭銆?/li>

浜屻€丗MT.PRINT鍑芥暟

fmt.Print 鍑芥暟鐢ㄤ簬鐩存帴杈撳嚭鍐呭锛屼笉甯︿换浣曟牸寮忓寲銆傝繖涓嚱鏁伴€傜敤浜庣畝鍗曠殑杈撳嚭鍦烘櫙銆備互涓嬫槸涓€涓ず渚嬶細

package main

import "fmt"

func main() {

fmt.Print("Hello, World!")

}

杩欐浠g爜灏嗚緭鍑?quot;Hello, World!"锛屼絾涓嶄細鑷姩鎹㈣銆?/p>

涓夈€丗MT.PRINTLN鍑芥暟

fmt.Println 鍑芥暟涓嶄粎鍙互杈撳嚭鍐呭锛岃繕浼氬湪鍐呭鏈熬娣诲姞涓€涓崲琛岀銆傝繖涓嚱鏁伴潪甯搁€傚悎闇€瑕佹崲琛岀殑杈撳嚭鍦烘櫙銆備互涓嬫槸涓€涓ず渚嬶細

package main

import "fmt"

func main() {

fmt.Println("Hello, World!")

}

杩欐浠g爜灏嗚緭鍑?quot;Hello, World!"锛屽苟鍦ㄥ叾鍚庤嚜鍔ㄦ崲琛屻€?/p>

鍥涖€丗MT.PRINTF鍑芥暟

fmt.Printf 鍑芥暟鐢ㄤ簬鏍煎紡鍖栬緭鍑恒€傚畠鎺ュ彈涓€涓牸寮忓寲瀛楃涓插拰涓€绯诲垪鍙傛暟銆傛牸寮忓寲瀛楃涓蹭腑鍙互鍖呭惈鍗犱綅绗︼紝杩欎簺鍗犱綅绗﹀皢琚弬鏁版浛鎹€備互涓嬫槸涓€涓ず渚嬶細

package main

import "fmt"

func main() {

name := "John"

age := 30

fmt.Printf("Name: %s, Age: %d\n", name, age)

}

杩欐浠g爜灏嗚緭鍑?quot;Name: John, Age: 30"锛屽苟鍦ㄥ叾鍚庤嚜鍔ㄦ崲琛屻€?/p>

浜斻€佸崰浣嶇璇﹁В

鍦ㄤ娇鐢?strong>fmt.Printf 鍑芥暟鏃讹紝鍗犱綅绗︽槸闈炲父閲嶈鐨勩€備互涓嬫槸涓€浜涘父鐢ㄧ殑鍗犱綅绗︼細

  • %s锛氬瓧绗︿覆
  • %d锛氭暣鏁?/li>
  • %f锛氭诞鐐规暟
  • %t锛氬竷灏斿€?/li>
  • %v锛氶粯璁ゆ牸寮?/li>

鍗犱綅绗﹀彲浠ュ府鍔╂垜浠洿鐏垫椿鍦版牸寮忓寲杈撳嚭鍐呭銆備緥濡傦細

package main

import "fmt"

func main() {

name := "Alice"

age := 25

height := 1.68

isStudent := true

fmt.Printf("Name: %s, Age: %d, Height: %.2f, Is Student: %t\n", name, age, height, isStudent)

}

杩欐浠g爜灏嗚緭鍑?quot;Name: Alice, Age: 25, Height: 1.68, Is Student: true"锛屽苟鍦ㄥ叾鍚庤嚜鍔ㄦ崲琛屻€?/p>

鍏€佹牸寮忓寲鏍囧織

fmt.Printf 鍑芥暟杩樻敮鎸佷竴浜涙牸寮忓寲鏍囧織锛岃繖浜涙爣蹇楀彲浠ユ洿绮剧粏鍦版帶鍒惰緭鍑烘牸寮忋€備緥濡傦細

  • 锛氬乏瀵归綈
  • +锛氭€绘槸杈撳嚭绗﹀彿
  • #锛氬€肩殑鏇夸唬鏍煎紡

浠ヤ笅鏄竴涓ず渚嬶細

package main

import "fmt"

func main() {

number := 123

fmt.Printf("|%6d|\n", number) // 榛樿鍙冲榻?/p>

fmt.Printf("|%-6d|\n", number) // 宸﹀榻?/p>

fmt.Printf("|%+6d|\n", number) // 鎬绘槸杈撳嚭绗﹀彿

}

杩欐浠g爜灏嗚緭鍑猴細

|   123|

|123 |

| +123 |

涓冦€佸疄渚嬭鏄?/h2>

浠ヤ笅鏄竴涓患鍚堜娇鐢?strong>fmt.Print銆?strong>fmt.Println 鍜?strong>fmt.Printf 鐨勭ず渚嬶細

package main

import "fmt"

func main() {

name := "Bob"

age := 22

// 浣跨敤fmt.Print

fmt.Print("Name: ")

fmt.Print(name)

fmt.Print(", Age: ")

fmt.Print(age)

fmt.Print("\n")

// 浣跨敤fmt.Println

fmt.Println("Name:", name, ", Age:", age)

// 浣跨敤fmt.Printf

fmt.Printf("Name: %s, Age: %d\n", name, age)

}

杩欐浠g爜灏嗕緷娆¤緭鍑猴細

Name: Bob, Age: 22

Name: Bob , Age: 22

Name: Bob, Age: 22

鍏€佹€荤粨涓庡缓璁?/h2>

鎬荤粨鏉ヨ锛孏o璇█涓殑杈撳嚭绗﹀彿涓昏鍖呮嫭fmt.Print銆?strong>fmt.Println 鍜?strong>fmt.Printf 涓夌銆傝繖浜涘嚱鏁板悇鏈夌壒鐐癸紝閫傜敤浜庝笉鍚岀殑鍦烘櫙銆傚湪瀹為檯搴旂敤涓紝鍙互鏍规嵁鍏蜂綋闇€姹傞€夋嫨鍚堥€傜殑杈撳嚭鍑芥暟銆備互涓嬫槸涓€浜涘缓璁細

  1. 绠€鍗曡緭鍑?/strong>锛氫娇鐢?strong>fmt.Print銆?/li>
  2. 甯︽崲琛岃緭鍑?/strong>锛氫娇鐢?strong>fmt.Println銆?/li>
  3. 鏍煎紡鍖栬緭鍑?/strong>锛氫娇鐢?strong>fmt.Printf銆?/li>

閫氳繃鐏垫椿浣跨敤杩欎簺杈撳嚭鍑芥暟锛屽彲浠ユ洿楂樻晥鍦拌繘琛孏o璇█缂栫▼銆傚笇鏈涜繖浜涗俊鎭兘甯姪浣犳洿濂藉湴鐞嗚В鍜屽簲鐢℅o璇█鐨勮緭鍑虹鍙枫€?/p>

鏇村闂瓟FAQs锛?/strong>

闂1锛欸o璇█涓殑杈撳嚭绗﹀彿鏄粈涔堬紵

鍦℅o璇█涓紝鐢ㄤ簬鏍囧噯杈撳嚭鐨勭鍙锋槸fmt.Println銆傝繖涓嚱鏁板彲浠ュ皢浼犲叆鐨勫弬鏁版墦鍗板埌鎺у埗鍙帮紝骞跺湪鏈熬娣诲姞涓€涓崲琛岀銆?/p>

闂2锛氬浣曞湪Go璇█涓緭鍑虹壒娈婄鍙凤紵

鍦℅o璇█涓紝濡傛灉瑕佽緭鍑虹壒娈婄鍙凤紝鍙互浣跨敤杞箟瀛楃鏉ヨ〃绀恒€備互涓嬫槸涓€浜涘父瑙佺殑鐗规畩绗﹀彿鍙婂叾杞箟瀛楃锛?/p>

  • 鎹㈣绗︼細\n
  • 鍒惰〃绗︼細\t
  • 鍥炶溅绗︼細\r
  • 鍙嶆枩鏉狅細\\
  • 鍙屽紩鍙凤細\"
  • 鍗曞紩鍙凤細\'

渚嬪锛屽鏋滆鍦ㄨ緭鍑轰腑鎻掑叆涓€涓崲琛岀锛屽彲浠ヤ娇鐢?code>fmt.Println("Hello\nWorld")銆?/p>

闂3锛氬浣曞湪Go璇█涓緭鍑篣nicode瀛楃锛?/strong>

Go璇█鏀寔Unicode瀛楃闆嗭紝鍥犳鍙互鐩存帴杈撳嚭Unicode瀛楃銆傚彲浠ヤ娇鐢?code>fmt.Println鍑芥暟灏哢nicode瀛楃鎵撳嵃鍒版帶鍒跺彴銆?/p>

渚嬪锛岃杈撳嚭涓€涓瑧鑴哥鍙凤紙Unicode缂栫爜涓篣+1F600锛夛紝鍙互浣跨敤fmt.Println("馃榾")銆?/p>

杩樺彲浠ヤ娇鐢?code>fmt.Printf鍑芥暟鏉ユ牸寮忓寲杈撳嚭锛屼互鏀寔鏇村鐨勮緭鍑洪€夐」銆備緥濡傦紝瑕佽緭鍑轰竴涓甫鏈塙nicode缂栫爜鐨勫瓧绗︼紝鍙互浣跨敤fmt.Printf("%c", rune)锛屽叾涓?code>rune鏄疷nicode瀛楃鐨勭被鍨嬨€?/p>

浠ヤ笂鏄叧浜嶨o璇█杈撳嚭绗﹀彿鐨勪竴浜涘父瑙侀棶棰樼殑瑙g瓟锛屽笇鏈涘鎮ㄦ湁甯姪锛?/p>



相关内容FAQs: