Go Printing to Stdout and Stderr: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
 
(20 intermediate revisions by the same user not shown)
Line 2: Line 2:
* [[Go_Language#Printing|Go Language]]
* [[Go_Language#Printing|Go Language]]
* [[Go Code Examples#Code_Examples|Go Code Examples]]
* [[Go Code Examples#Code_Examples|Go Code Examples]]
* [[Go Package fmt|The <tt>fmt</tt> Package]]
=Bootstrapping Functions=
=Bootstrapping Functions=
Both <code>[[#print.28.29|print()]]</code> and <code>[[#println.28.29|println()]]</code> are [[Go_Language#Pre-Declared_Functions|pre-declared functions]], ready to use without any import.
Both <code>[[#print.28.29|print()]]</code> and <code>[[#println.28.29|println()]]</code> are [[Go_Language#Pre-Declared_Functions|pre-declared functions]], ready to use without any import.
<font color=darkkhaki>TODO: https://golang.org/ref/spec#Bootstrapping</font>
==<tt>print()</tt>==
==<tt>print()</tt>==
==<tt>println()</tt>==
==<tt>println()</tt>==
Line 12: Line 16:
</syntaxhighlight>
</syntaxhighlight>


=<tt>fmt.Printf()</tt>=
=<tt>fmt</tt> Functions=
{{External|https://golang.org/pkg/fmt/#Printf fmt.Printf()}}
{{Internal|Go_Package_fmt|The <tt>fmt</tt> Package}}
==<tt>fmt.Printf()</tt>==
{{External|https://golang.org/pkg/fmt/#Printf}}
<syntaxhighlight lang='go'>
<syntaxhighlight lang='go'>
import "fmt"
import "fmt"


// ...
// ...
fmt.Printf("something")
fmt.Printf("something %s", "blue")
</syntaxhighlight>
</syntaxhighlight>
 
<code>Print()</code> expects a '''format specifier''' (or a '''format string''') as the first argument, which contains [[Go_Package_fmt#Conversion_Characters|conversion characters]] (ex. <code>%s</code>). {{Internal|Go_Package_fmt#Conversion_Characters|Format String &#124; Conversion Characters}}
==Format Strings==
The format strings contain conversion characters (ex. <code>%s</code>):
 
 
 
<syntaxhighlight lang='go'>
fmt.Printf("Hello %s", "Bob")
</syntaxhighlight>
 
 
 
 
Prints according to a format specifier. Format specifier documentation https://golang.org/pkg/fmt
 
===Pointers===
 
<pre>
fmt.Printf("%p\n", sPtr)
</pre>
 
"%p" prepend the "0x" prefix.
 
Pointers can be also represented using "%X" (base 16, upper case characters).
 
===Ints===
 
<pre>
fmt.Printf("%d\n",i)
</pre>
 
===Characters===
 
<pre>
fmt.Printf("%c\n",i)
</pre>
 
===Boolean Values===
 
<pre>
fmt.Printf("%t\n",b)
</pre>

Latest revision as of 22:00, 22 December 2023

Internal

Bootstrapping Functions

Both print() and println() are pre-declared functions, ready to use without any import.

TODO: https://golang.org/ref/spec#Bootstrapping

print()

println()

func main() {
  println("something")
}

fmt Functions

The fmt Package

fmt.Printf()

https://golang.org/pkg/fmt/#Printf
import "fmt"

// ...
fmt.Printf("something %s", "blue")

Print() expects a format specifier (or a format string) as the first argument, which contains conversion characters (ex. %s).

Format String | Conversion Characters