go语言如何打印符号及其用法揭秘
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 涓夌銆傝繖浜涘嚱鏁板悇鏈夌壒鐐癸紝閫傜敤浜庝笉鍚岀殑鍦烘櫙銆傚湪瀹為檯搴旂敤涓紝鍙互鏍规嵁鍏蜂綋闇€姹傞€夋嫨鍚堥€傜殑杈撳嚭鍑芥暟銆備互涓嬫槸涓€浜涘缓璁細
- 绠€鍗曡緭鍑?/strong>锛氫娇鐢?strong>fmt.Print銆?/li>
- 甯︽崲琛岃緭鍑?/strong>锛氫娇鐢?strong>fmt.Println銆?/li>
- 鏍煎紡鍖栬緭鍑?/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>