在测试 golang 的 println 和 fmt.println 时需要帮助

need help while tesing println and fmt.println of golang

我写了简单的代码来测试 println 和 fmt.Println,但是当我 运行 代码时,它几乎每次都打印不同的结果。我试图 google println 和 fmt.Println 之间的区别,但一无所获。有没有人知道这两个功能的真正区别或优先级或顺序?

代码如下:

package main
import (
    "fmt"
)

func main(){
    println("a")
    fmt.Println("b")
    println("c")
    fmt.Println("d")

    p()
}

func p(){
    println("e")
    fmt.Println("f")
    println("g")
    fmt.Println("h")
}

谢谢!

Builtin functions

func println

func println(args ...Type)

The println built-in function formats its arguments in an implementation- specific way and writes the result to standard error. Spaces are always added between arguments and a newline is appended. Println is useful for bootstrapping and debugging; it is not guaranteed to stay in the language.

Package fmt

func Println

func Println(a ...interface{}) (n int, err error)

Println formats using the default formats for its operands and writes to standard output. Spaces are always added between operands and a newline is appended. It returns the number of bytes written and any write error encountered.

fmt.Println() 使用 stdoutprintln() 使用 stderr.

正如预期的那样,两个不同目的的不同函数给出了不同的结果。

最佳做法:仅使用 fmt.Println("My text...")

不使用println("My text...")