1. 输出
1.1 Print类
func Print(a ...any) (n int, err error)
普通输出,默认不带换行。
package main
import (
"fmt"
)
func main() {
const name, age = "Kim", 22
fmt.Print(name, " is ", age, " years old.\n")
// It is conventional not to worry about any
// error returned by Print.
}
Output:
Kim is 22 years old.
func Printf(format string, a ...any) (n int, err error)
格式化输出,默认不带换行。
package main
import (
"fmt"
)
func main() {
const name, age = "Kim", 22
fmt.Printf("%s is %d years old.\n", name, age)
// It is conventional not to worry about any
// error returned by Printf.
}
Output:
Kim is 22 years old.
func Println(a ...any) (n int, err error)
普通输出,带换行。
package main
import (
"fmt"
)
func main() {
const name, age = "Kim", 22
fmt.Println(name, "is", age, "years old.")
// It is conventional not to worry about any
// error returned by Println.
}
Output:
Kim is 22 years old.
- 总结:
此类是最普通的输出,记住如何格式化输出和默认带换行。
1.2 Sprint类
func Sprint(a ...any) string
将普通输出转化为字符串返回,默认不带换行。
package main
import (
"fmt"
"io"
"os"
)
func main() {
const name, age = "Kim", 22
s := fmt.Sprint(name, " is ", age, " years old.\n")
io.WriteString(os.Stdout, s) // Ignoring error for simplicity.
}
Output:
Kim is 22 years old.
func Sprintf(format string, a ...any) string
将格式化输出转化为字符串返回,默认不带换行。
package main
import (
"fmt"
"io"
"os"
)
func main() {
const name, age = "Kim", 22
s := fmt.Sprintf("%s is %d years old.\n", name, age)
io.WriteString(os.Stdout, s) // Ignoring error for simplicity.
}
Output:
Kim is 22 years old.
func Sprintln(a ...any) string
将普通输出转化为字符串返回,默认带换行。
package main
import (
"fmt"
"io"
"os"
)
func main() {
const name, age = "Kim", 22
s := fmt.Sprintln(name, "is", age, "years old.")
io.WriteString(os.Stdout, s) // Ignoring error for simplicity.
}
Output:
Kim is 22 years old.
- 总结
此类是Print类的延伸,不直接输出,而是返回字符串。
1.3 Fprint类
func Fprint(w io.Writer, a ...any) (n int, err error)
将普通输出通过Writer
输出,返回的是输出字节数和error。
package main
import (
"fmt"
"os"
)
func main() {
const name, age = "Kim", 22
n, err := fmt.Fprint(os.Stdout, name, " is ", age, " years old.\n")
// The n and err return values from Fprint are
// those returned by the underlying io.Writer.
if err != nil {
fmt.Fprintf(os.Stderr, "Fprint: %v\n", err)
}
fmt.Print(n, " bytes written.\n")
}
Output:
Kim is 22 years old.
21 bytes written.
func Fprintf(w io.Writer, format string, a ...any) (n int, err error)
将格式化输出通过Writer
输出,返回的是输出字节数和error。
package main
import (
"fmt"
"os"
)
func main() {
const name, age = "Kim", 22
n, err := fmt.Fprintf(os.Stdout, "%s is %d years old.\n", name, age)
// The n and err return values from Fprintf are
// those returned by the underlying io.Writer.
if err != nil {
fmt.Fprintf(os.Stderr, "Fprintf: %v\n", err)
}
fmt.Printf("%d bytes written.\n", n)
}
Output:
Kim is 22 years old.
21 bytes written.
func Fprintln(w io.Writer, a ...any) (n int, err error)
将普通输出通过Writer
输出,默认带换行,返回的是输出字节数和error。
package main
import (
"fmt"
"os"
)
func main() {
const name, age = "Kim", 22
n, err := fmt.Fprintln(os.Stdout, name, "is", age, "years old.")
// The n and err return values from Fprintln are
// those returned by the underlying io.Writer.
if err != nil {
fmt.Fprintf(os.Stderr, "Fprintln: %v\n", err)
}
fmt.Println(n, "bytes written.")
}
Output:
Kim is 22 years old.
21 bytes written.
- 总结
此类是Print类的延伸,不直接输出,通过Writer输出,似乎一般用不到。
2. 输入
2.1 Scan类
func Scan(a ...any) (n int, err error)
用于接受任意个输入,不同输入用空格分割,换行当作空格处理。
package main
import "fmt"
func main(){
var a, b int
n, err := fmt.Scan(&a, &b)
if err != nil {
fmt.Println(n, a, b)
}
}
func Scanf(format string, a ...any) (n int, err error)
格式化接受输入,用空格分割,可以换行,输入中的换行必须和format中的换行匹配。
package main
import "fmt"
func main(){
fomat := "%f / %d / %s"
var a float32
var b int
var c string
n, err := fmt.Scan(&a, &b, &s)
}
func Scanln(a ...any) (n int, err error)
和Scna
类似,但是只接收一行的输入,用空格分割。
- 总结
最普通的接收输入。
2.2 Sscan类
与Scan类基本一致,只不过多了一个参数str String
,代表从这个字符串中接收输入。
2.3 Fscan类
与Scan类基本一致,只不过多了一个参数r io.Reader
,代表从这个Reader中接收输入。