Documentation
¶
Overview ¶
What is Type Assertion?
As the name suggests they are used to assert that a variable is of some type.
Type assertions are used to check if the value held by the interface type variable either implements the desired interface (satisfies) or is of a concrete type.
They can only take place on interfaces.
Type assertions are performed at runtime.
x.(T) T is a concrete type or interface One (asserted value) or two (ok) values can be returned
Why would I use one? What exactly do they return?
x.(T) assert that x is not nil and that the value stored in x is type of T
Why would I use them?
- to check that x is nil
- to check if it is convertible (assert) to another type
- convert (assert) it to another type
What exactly do they return?
t := x.(T) => is of type T; if x is nil, it panics t, ok := x.(t) => if x is nil or not of type T => ok is false otherwise ok is true and t is of type T
vs. Type Conversion
Guess Game
- Generate a random number from 1 to 100, store it as a target number for the player to guess.
- Prompt the player to guess what the target number is, storing their response.
- If the guess is less than the target, return "Your guess is too LOW." If the player's guess is greater than the target number, return, "Your guess is too HIGH."
- Allow the player to guess up to 10 times. Before each guess, let the player know how many guesses they have left.
- If the player's guess is equal to the target number, return "Your guess is correct!" and stop asking for guesses.
- If the player ran out of turns without correctly guessing the target number, return, "Sorry, your attempts were unsuccessful. It was [X]" and return the target number.
Source Files
¶
Directories
¶
Path | Synopsis |
---|---|
design-patterns
|
|
examples
|
|
Exercise: 1.
|
Exercise: 1. |
functions
Print the size of a file.
|
Print the size of a file. |
types
Exercise: 1.
|
Exercise: 1. |
types/numbers
Go-Pherit: A Learning Experience and personal SDK in Golang Copyright (c) 2020 Rye Miller https://darksociety.io
|
Go-Pherit: A Learning Experience and personal SDK in Golang Copyright (c) 2020 Rye Miller https://darksociety.io |
Golph Calculates the average of several numbers.
|
Golph Calculates the average of several numbers. |
Click to show internal directories.
Click to hide internal directories.