MCPcopy Create free account
hub / github.com/mmcgrana/gobyexample / main

Function main

examples/string-formatting/string-formatting.go:16–110  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

14}
15
16func main() {
17
18 // Go offers several printing "verbs" designed to
19 // format general Go values. For example, this prints
20 // an instance of our `point` struct.
21 p := point{1, 2}
22 fmt.Printf("struct1: %v\n", p)
23
24 // If the value is a struct, the `%+v` variant will
25 // include the struct's field names.
26 fmt.Printf("struct2: %+v\n", p)
27
28 // The `%#v` variant prints a Go syntax representation
29 // of the value, i.e. the source code snippet that
30 // would produce that value.
31 fmt.Printf("struct3: %#v\n", p)
32
33 // To print the type of a value, use `%T`.
34 fmt.Printf("type: %T\n", p)
35
36 // Formatting booleans is straight-forward.
37 fmt.Printf("bool: %t\n", true)
38
39 // There are many options for formatting integers.
40 // Use `%d` for standard, base-10 formatting.
41 fmt.Printf("int: %d\n", 123)
42
43 // This prints a binary representation.
44 fmt.Printf("bin: %b\n", 14)
45
46 // This prints the character corresponding to the
47 // given integer.
48 fmt.Printf("char: %c\n", 33)
49
50 // `%x` provides hex encoding.
51 fmt.Printf("hex: %x\n", 456)
52
53 // There are also several formatting options for
54 // floats. For basic decimal formatting use `%f`.
55 fmt.Printf("float1: %f\n", 78.9)
56
57 // `%e` and `%E` format the float in (slightly
58 // different versions of) scientific notation.
59 fmt.Printf("float2: %e\n", 123400000.0)
60 fmt.Printf("float3: %E\n", 123400000.0)
61
62 // For basic string printing use `%s`.
63 fmt.Printf("str1: %s\n", "\"string\"")
64
65 // To double-quote strings as in Go source, use `%q`.
66 fmt.Printf("str2: %q\n", "\"string\"")
67
68 // As with integers seen earlier, `%x` renders
69 // the string in base-16, with two output characters
70 // per byte of input.
71 fmt.Printf("str3: %x\n", "hex this")
72
73 // To print a representation of a pointer, use `%p`.

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected