Go is a statically typed language.
The interpreter cares which value we are assigning to a variable.
- C++
- Java
- Go
...
// this would error in Go
var test string = 5
...bool- true/falsestring- "hi"int- 0, -10000, 99999float64- 10.00001, 0.0008, -100.003
The interpreter does not care which value we are assigning to a variable.
- Javascript
- Ruby
- Python
...
// this would not error in Javascript
var test = 5 ;
test = 'string';
...The most basic way to declare a variable in Go can be seen below. This way of declaring a variable can be done inside or outside of a function
var card string = "Ace of Spades"The shorthand of this can only be done inside of a function:
func main() {
card := "Ace of Spades"
}You cannot reassign a variable to a different type. The below example would error:
func main() {
card := "Ace of Spades"
card = 5 // error
}You have to declare what type a function returns
func newCard() string {
return "Five of Diamonds"
}Go has two type of lists:
array- fixed length list of thingsslice- an array that can shrink or grow
Every element in a slice must be of the same type
You must declare the type of the elements within a slice.
func main() {
cards := []string{"Ace of Diamonds", "Six of Spades"}
...
}You can use the append method to add a new element to the end of a slice.
func main() {
cards := []string{"Ace of Diamonds", "Six of Spades"}
cards = append(cards, "King of Hearts")
...
}func main() {
cards := []string{"Ace of Diamonds", "Six of Spades"}
cards = append(cards, "King of Hearts")
for index, card := range cards {
// do stuff with index and card
}
}You can use syntax like [0:2] or [1:4] or [:2] or [2:] to access ranges of indexes. The first number is the starting position and is includes, the second number is the ending position and is not included.
...
cards := []string{"Ace of Diamonds", "Six of Spades", "Queen of Hearts"}
hand := cards[:2] // would give me the cards indexes 0 through 1
hand2 := cards[1:3] // would give me the cards indexes 1 through 2- Declare a new type and set it to whatever existing type you like (slice of strings in this case)
- add a method to the type
deckcalledprint, the receiver takes two variables:dwhich is similar tothisin other languages anddeckwhich is the type you're adding the method to. By convention the first variable is a 1 or 2 letter variable matching the second variable's starting letters. - Init an instance of the type, setting it to a variable, then you can access the methods.
...
type deck []string
func (d deck) print() {
for i, card := range d {
fmt.Println(i, card)
}
}
...
func main() {
cards := deck{"Ace of Diamonds", "Six of Spades"}
cards.print()
}- Has a receiver of
dwith type deck - Function takes one argument
handSizewith type int - It returns two things, both with type deck
type deck []string
func deal(d deck, handSize int) (deck, deck) {
return d[:handSize], d[handSize:]
}
...
func main() {
cards := newDeck()
hand, remainingDeck := deal(cards, 5)
}You can see available packages in Go here: https://golang.org/pkg/
You can use the package ioutil to write files to your hard drive, specifically the WriteFile function https://golang.org/pkg/io/ioutil/#WriteFile
- It takes a filename with type string
- It takes data which is a slice of bytes (aka a "byte slice" in Go)
- It also takes a perm (short for permissions)
func WriteFile(filename string, data []byte, perm os.FileMode) errorA byte slice is a slice of numbers that coorispond to a string. For example the string Hi there! as a byte slice looks like this: [72 105 32 116 104 101 114 101 33]. This is just a computer fiendly way to write a string.
You can see at http://www.asciitable.com/ a table that you can look up bytes to letters.
You can change a type in Go
<type-you-want>(<value-you-have>)
This example will covert "Hello there!" into a byte slice:
greeting := "Hi There!"
greetingBytes := []byte(greeting)Always end with _test.go
The basic structure for Go testing is to
- Name a function that has a name of
Test<name-of-func-being-tested>, this will make it easier to find - This is always called with the a param
twith type of*testing.T - You call the method you'd like to test
- setup conditionals that check expected behavior
- Use the
tparam to to call an error witht.Errorf - Run
$ go testOR use VSCode to run individual test in the code editor
func TestNewDeck(t *testing.T) {
d := newDeck()
if len(d) != 52 {
t.Errorf("Expected deck length of 52, but got %v", len(d))
}
if d[0] != "Ace of Spades" {
t.Errorf("Expected Ace of Spades to be first, but got %v", d[0])
}
if d[len(d)-1] != "King of Clubs" {
t.Errorf("Expected King of Clubs ot be first, but got %v", d[len(d)-1])
}
}