Go Printing to Stdout and Stderr: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
 
(14 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]]
* [[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 13: Line 15:
}
}
</syntaxhighlight>
</syntaxhighlight>
=<tt>fmt</tt> Functions=
=<tt>fmt</tt> Functions=
 
{{Internal|Go_Package_fmt|The <tt>fmt</tt> Package}}
=<tt>fmt.Printf()</tt>=
==<tt>fmt.Printf()</tt>==
{{External|https://golang.org/pkg/fmt/#Printf}}
{{External|https://golang.org/pkg/fmt/#Printf}}
<syntaxhighlight lang='go'>
<syntaxhighlight lang='go'>
Line 23: Line 26:
fmt.Printf("something %s", "blue")
fmt.Printf("something %s", "blue")
</syntaxhighlight>
</syntaxhighlight>
<code>Print()</code> expects a format specifier (or a format string) as the first argument, which contains conversion characters (ex. <code>%s</code>):
<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}}
===Strings===
<syntaxhighlight lang='go'>
color := "blue"
fmt.Printf("The color is %s\n", color)
</syntaxhighlight>
===Integers===
<syntaxhighlight lang='go'>
size := 1
fmt.Printf("The size is %d\n", size)
</syntaxhighlight>
===Characters===
<syntaxhighlight lang='go'>
c := 'x'
fmt.Printf("The character is %c\n", c)
</syntaxhighlight>
===Booleans===
<syntaxhighlight lang='go'>
b := true
fmt.Printf("The boolean value is %t\n", b)
</syntaxhighlight>
===Floating Point Numbers===
<syntaxhighlight lang='go'>
f := 1.0
fmt.Printf("The floating point value is %f\n", f)
</syntaxhighlight>
 
===Pointers===
<syntaxhighlight lang='go'>
s := "something"
sPtr := &s
fmt.Printf("pointer value: %p\n", sPtr)
</syntaxhighlight>
 
<code>"%p"</code> prepend the "0x" prefix.
 
Pointers can be also represented using <code>"%X"</code> (base 16, upper case characters).

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