Go
Credit goes to - Master the fundamentals and advanced features of the Go Programming Language (Golang), on Udemy by Stephen Grider.
I have tried some snippets on my own and some are referring to the snippets developed while going through the course.
Static Typed languages
While JS, Python etc are dynamic languages having no concern about type of values assigned
Basic Go Types
- - - - - - - - - - -
bool, string, int, float64
Go doesn’t follow OOPs like Java
Steps
- - - -
Install Go
Install Editor VSCode / Atom, etc/
Hello World
---------------
package main
import "fmt"
func main() {
fmt.Println("hello world")
}
Common commands
------------------------
go build
go run
go fmt
go install
go get
go test
$ go run main.go
hello world
$ go build main.go
$ ls
main main.go
$ ./main
hello world
Packages
------------
2 types -
executable : creates and executable file like ./main, name 'main' makes sure its executable, should have a function defined with name as 'main' as well
reusable: some helper codes, with any name other than 'main'
Imports
----------
fmt - basic library, others are debug, math, cypto, encoding, etc.
Functions
------------
func - for function
Variables
------------
var card string = "hello"
or
card := "hello again"
card = "new value" // no need for colon as only needed for first time declaration
package main
import "fmt"
var num int
func main() {
var card0 string = "Hello World"
card := "Hello Again World"
fmt.Println(card0)
fmt.Println(card)
fmt.Println(num)
}
$ ./main
Hello World
Hello Again World
0
Note: Can declare a variable outside main(), just cant assign the value
Functions
-------------
func newFunc() string {
return "new Hello"
}
Note: if datatypes is not defined - too many arguments to return
package main
import "fmt"
var num int
func main() {
var vari0 string = "Hello World"
vari1 := "Hello Again World"
fmt.Println(vari0)
fmt.Println(vari1)
fmt.Println(num)
vari2 := newFunc()
fmt.Println(vari2)
}
func newFunc() string {
return "new Hello"
}
Data Structures in Go
----------------------------
Array - Fixed length
Slice - Dynamic Array
Both should have a data type defined, and the elements should be of tne same type
Appending a slice and iterating -
package main
import "fmt"
var num int
func main() {
varis := []string{"Hello0 ", newFunc()}
fmt.Println(varis)
varis = append(varis, "Hello2")
fmt.Println(varis)
for i, vari := range varis {
fmt.Println(i, vari)
}
}
:= because with every increment, previous value is discarded
Receivers
-------------
stack.go
package main
import "fmt"
type stack[] string
func (s stack) print() {
for i, str := range s {
fmt.Println(i, str)
}
}
main.go
...
func main () {
...
stacks := stack{"stk1", "stk2", "stk3"}
stacks = append(stacks, "stk4")
for i, stack := range stacks {
fmt.Println(i, stack)
}
...
}
o/p :
0 stk1
1 stk2
2 stk3
3 stk4
Nested Loops
-----------------
deck.go
- - - -
func newDeck() deck {
cards := deck{"Ace of Spades"} // how to cover all combo
cardSuits := []string {"Spades", "Diamonds", "Hearts", "Club"}
cardValues := []string {"Ace", "Two", "Three", "Four", "Five" , "Six", "Seven", "Eight", "Nine" , "Ten", "Jack", "Queen", "King" }
for _, suit := range cardSuits { // instead of i, j use _ so that no compulsion to use
for _, value := range cardValues {
cards = append(cards, suit + " of " + value)
}
}
return cards
}
main.go
- - - -
...
cards = newDeck()
for i, card := range cards {
fmt.Println(i, card) // because cards is of type deck, hence print() is available
}
...
o/p
0 Ace of Spades
1 Spades of Ace
2 Spades of Two
3 Spades of Three
4 Spades of Four
5 Spades of Five
6 Spades of Six
7 Spades of Seven
8 Spades of Eight
9 Spades of Nine
10 Spades of Ten
11 Spades of Jack
12 Spades of Queen
13 Spades of King
14 Diamonds of Ace
15 Diamonds of Two
16 Diamonds of Three
17 Diamonds of Four
18 Diamonds of Five
19 Diamonds of Six
20 Diamonds of Seven
21 Diamonds of Eight
22 Diamonds of Nine
23 Diamonds of Ten
24 Diamonds of Jack
25 Diamonds of Queen
26 Diamonds of King
27 Hearts of Ace
28 Hearts of Two
29 Hearts of Three
30 Hearts of Four
31 Hearts of Five
32 Hearts of Six
33 Hearts of Seven
34 Hearts of Eight
35 Hearts of Nine
36 Hearts of Ten
37 Hearts of Jack
38 Hearts of Queen
39 Hearts of King
40 Club of Ace
41 Club of Two
42 Club of Three
43 Club of Four
44 Club of Five
45 Club of Six
46 Club of Seven
47 Club of Eight
48 Club of Nine
49 Club of Ten
50 Club of Jack
51 Club of Queen
52 Club of King
Slices
-------
Chunks of Slice
slice_temp[0:2] - including 0th but before 2, excluding 2
used for getting subsets
Example using multiple returns
- - - - - - - - - - - - - - - - - - - - - - - -
deck.go
- - - -
func deal (d deck, handsize int) (deck, deck) { // multiple returns of type deck each
return d[:handsize], d[handsize:]
}
main.go
- - - -
...
fmt.Println("########## sliced decks #########")
hand, remainingCards := deal(cards, 5) // hand will have first sliced deck and remaining will go in the later
fmt.Println("hand :: ")
hand.print()
fmt.Println("remainingCards :: ")
remainingCards.print()
...
o/p
hand ::
0 Ace of Spades
1 Spades of Ace
2 Spades of Two
3 Spades of Three
4 Spades of Four
remainingCards ::
0 Spades of Five
1 Spades of Six
2 Spades of Seven
3 Spades of Eight
4 Spades of Nine
.......
Watch out for Part 2. 👍
Regards
TheCodeBuddy
No comments:
Post a Comment