Last active
August 18, 2017 17:19
-
-
Save aliaspooryorik/4e61dcf63cabfbd503ae15ebd053de75 to your computer and use it in GitHub Desktop.
Go : Example 1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
) | |
func main() { | |
text := `The quick brown fox jumps over the lazy dog` | |
vowels, spaces, constants := 0, 0, 0 | |
for _, char := range text { | |
switch char { | |
case 'a', 'e', 'i', 'o', 'u': | |
vowels++ | |
case ' ': | |
spaces++ | |
default: | |
constants++ | |
} | |
} | |
fmt.Printf("Found %d vowels, %d spaces and %d constants", vowels, spaces, constants) | |
} | |
------ | |
Loops | |
------ | |
package main | |
import ( | |
"fmt" | |
) | |
func main() { | |
stop := false | |
i := 0 | |
for !stop { | |
i++ | |
stop = i == 5 | |
fmt.Printf("Counter is at %d\n", i) | |
} | |
i = 0 | |
for i := 0; i < 5; i++ { | |
fmt.Printf("Counter is at %d\n", i) | |
} | |
i = 0 | |
for i, j := 0, 1; i < 5; i, j = i+1, j*2 { | |
fmt.Printf("Counter is at %d j = %d\n", i, j) | |
} | |
} | |
----- | |
function args and return values | |
----- | |
package main | |
import ( | |
"fmt" | |
) | |
func main() { | |
starting() | |
i := printer("hello", "world!") | |
fmt.Printf("wrote out %d lines", i) | |
msg, x := complete() | |
fmt.Printf("%s %d", msg, x) | |
} | |
func starting() { | |
printer("start") | |
} | |
func complete() (string, int) { | |
msg := "complete\n"; | |
result := printer(msg) | |
return msg, result | |
} | |
func printer(msgs ...string) (counter int) { | |
counter = 0 | |
for i, msg := range msgs { | |
counter++ | |
fmt.Printf(" %d: %s\n", i, msg) | |
} | |
return counter | |
} | |
------ | |
array (slice) | |
------ | |
package main | |
import ( | |
"fmt" | |
) | |
func main() { | |
// this is a slice and is passed by reference | |
words := []string{"the", "quick", "brown", "fox"} | |
printer(words) | |
printer(words[0:1]) | |
words[0] = "!!The!!" | |
printer(words[0:1]) | |
printer(words[1:]) | |
newarray := append(words, "NEW") | |
printer(words) | |
printer(newarray) | |
} | |
func printer(words []string) { | |
for _, word := range words { | |
fmt.Printf("%s", word) | |
} | |
words[0] = "**The**" | |
} | |
---------- | |
maps | |
---------- | |
package main | |
import ( | |
"fmt" | |
) | |
func main() { | |
// this is a slice and is passed by reference | |
months := map[string]int{ | |
"jan": 31, | |
"feb": 28, | |
} | |
printDaysInMonth(months, "jan") | |
printDaysInMonth(months, "mar") | |
months["mar"] = 31 | |
printDaysInMonth(months, "mar") | |
printDaysInMonth(months, "Mar") | |
printEachMonth(months) | |
} | |
func printDaysInMonth(months map[string]int, key string) { | |
daysInMonth, ok := months[key] | |
if !ok { | |
fmt.Printf("key %s not found\n", key) | |
} else { | |
fmt.Printf("Days in month %s is %d\n", key, daysInMonth) | |
} | |
} | |
func printEachMonth(months map[string]int) { | |
for key, daysInMonth := range months { | |
fmt.Printf("month %s has %d days\n", key, daysInMonth) | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment