Golang input/output: Difference between revisions

From wikinotes
No edit summary
Line 1: Line 1:
= Documentation =
<blockquote>
{| class="wikitable"
|-
| <code>io/ioutil</code> (read/write) || https://pkg.go.dev/io/ioutil@go1.18.3
|-
| <code>io/fs</code> (filesystem) || https://pkg.go.dev/io/fs@go1.18.3
|-
|}
</blockquote><!-- Documentation -->
= print =
= print =
<blockquote>
<blockquote>
Line 46: Line 57:


</blockquote><!-- stdin, stdout, stderr -->
</blockquote><!-- stdin, stdout, stderr -->
= Filesystem =
<blockquote>
</blockquote><!-- Filesystem -->


= Networking =
= Networking =

Revision as of 21:21, 6 June 2022

Documentation

io/ioutil (read/write) https://pkg.go.dev/io/ioutil@go1.18.3
io/fs (filesystem) https://pkg.go.dev/io/fs@go1.18.3

print

Basics

require "fmt"

fmt.Println("foo")                 // print to stdout with newline
fmt.Printf("%v", 123)              // print formatted string to stdout (no newline)
fmt.Fprintf(os.Stdout, "%v", 123)  // prints formatted string to writable object (ex. STDOUT, STERR, ..)

fmt.Sprintf("%v", 123)             // returns formatted string (no newline)

Format Syntax

Printf/Sprintf/Fprintf all take format specifiers.
See full docs here, but here's some really useful formats:

# general
%v  # value
%T  # type

# number-bases
%b  # binary
%x  # hex
%o  # octal
%d  # decimal

# number types
%i  # int
%f  # float

# strings
%s  # string
%q  # quoted/escaped go string
%c  # unicode-char for num

stdin, stdout, stderr

Filesystem

Networking

Sockets

HTTP