「Go言語」PrintとSprintメソッドで文字列を出力する

2022年1月20日

書式
1.Printメソッド
fmt.Print(“文字列\n")
2.変数名 :=fmt.Sprint(“東京 tokyo.\n")
fmt.Print(変数名)
文字列を出力します
使用例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
package main
import "fmt"
func main() {
fmt.Print("Printで文字列を出力\n")
fmt.Print("東京 tokyo.\n")
fmt.Print("study", "skill", "\n")
fmt.Print("study", "skill", "become", "\n")
fmt.Print("*******Sprintで文字列を出力**********\n")
cftA := fmt.Sprint("東京 tokyo.\n")
fmt.Print(cftA)
cftB := fmt.Sprint("study", "skill", "\n")
fmt.Print(cftB)
result := fmt.Sprint("study", "skill", "become", "\n")
fmt.Print(result)
}
package main import "fmt" func main() { fmt.Print("Printで文字列を出力\n") fmt.Print("東京 tokyo.\n") fmt.Print("study", "skill", "\n") fmt.Print("study", "skill", "become", "\n") fmt.Print("*******Sprintで文字列を出力**********\n") cftA := fmt.Sprint("東京 tokyo.\n") fmt.Print(cftA) cftB := fmt.Sprint("study", "skill", "\n") fmt.Print(cftB) result := fmt.Sprint("study", "skill", "become", "\n") fmt.Print(result) }
package main

import "fmt"

func main() {
   fmt.Print("Printで文字列を出力\n")
      fmt.Print("東京 tokyo.\n")
      fmt.Print("study", "skill", "\n")
      fmt.Print("study", "skill", "become", "\n")
      
   fmt.Print("*******Sprintで文字列を出力**********\n")
      cftA := fmt.Sprint("東京 tokyo.\n")
      fmt.Print(cftA)
      cftB := fmt.Sprint("study", "skill", "\n")
      fmt.Print(cftB)
      result := fmt.Sprint("study", "skill", "become", "\n")
      fmt.Print(result)
}

実行結果

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
C:\study\skill\golang>go run 114.go
Printで文字列を出力
東京 tokyo.
studyskill
studyskillbecome
*******Sprintで文字列を出力**********
東京 tokyo.
studyskill
studyskillbecome
C:\study\skill\golang>go run 114.go Printで文字列を出力 東京 tokyo. studyskill studyskillbecome *******Sprintで文字列を出力********** 東京 tokyo. studyskill studyskillbecome
C:\study\skill\golang>go run 114.go
Printで文字列を出力
東京 tokyo.
studyskill
studyskillbecome
*******Sprintで文字列を出力**********
東京 tokyo.
studyskill
studyskillbecome

 

Go言語

Posted by arkgame