Simple data types

// archive From the original Code Walker docs. Kept for reference, so some versions and commands may be out of date.

Simple data types means they only store one, that is a single value, there are four:

  • Strings
  • Numbers
  • Booleans
  • Errors

See the Standard Library docs here: https://pkg.go.dev/std

Strings#

Strings in Go represent one or more characters.

Interpreted use double quotes strings will honour the escape character:

interpreted stringtext
"this is an example \n of a string"

Raw strings don’t:

raw stringtext
`this is an example \n of a string`

Nor to raw string care about white space, so they can do multiple lines, for example:

raw stringtext
`this is an example
of a string`

We can also us Sprintf from the fmt package:

Example:

package main

import "fmt"

func main() {
	fname := "Richard"
	lname := "Walker"
	age := 49
	messageRate := 0.5
	isSubscribed := false
	message := "Know thyself."

	// Don't touch above this line

	userLog := fmt.Sprintf("Name: %s %s, Age: %d, Rate: %.1f, Is Subscribed: %t, Message: %s", fname, lname, age, messageRate, isSubscribed, message )

	// Don't touch below this line

	fmt.Println(userLog)
}

Numbers#

Number in Go come in 4 flavours:

  • Integers (1, 42, -42) - int
  • Unsigned integers (0, 32, 8080) - uint
  • Floating point numbers (5.01e20, 3.1415) - float32 & float64
  • Complex numbers (0.833i) - complex64 & complex128

Booleans#

Boolean is either true or false.

Error#

A conventional interface representing an error condition.

type error interface {
    Error() string
}