@ -0,0 +1,36 @@ | |||
package main | |||
import ( | |||
"fmt" | |||
"os" | |||
) | |||
func getPassedArgs() string { | |||
if len(os.Args) != 2 { | |||
fmt.Printf("Exactly 1 argument is needed\n") | |||
os.Exit(1) | |||
} | |||
return os.Args[1] | |||
} | |||
func main() { | |||
m := map[string]string{ | |||
"305": "Sue", | |||
"204": "Bob", | |||
"631": "Jake", | |||
"073": "Tracy", | |||
} | |||
key := getPassedArgs() | |||
if name := m[key]; name != "" { | |||
fmt.Println("Hi,", name) | |||
} else { | |||
fmt.Println(key, "not found") | |||
os.Exit(1) | |||
} | |||
} |
@ -0,0 +1,57 @@ | |||
package main | |||
import ( | |||
"fmt" | |||
"os" | |||
"strings" | |||
) | |||
type locale struct { | |||
language string | |||
region string | |||
} | |||
func getLocales() map[locale]struct{} { | |||
supportedLocales := make(map[locale]struct{}, 5) | |||
supportedLocales[locale{"en", "US"}] = struct{}{} | |||
supportedLocales[locale{"en", "CN"}] = struct{}{} | |||
supportedLocales[locale{"fr", "CN"}] = struct{}{} | |||
supportedLocales[locale{"fr", "FR"}] = struct{}{} | |||
supportedLocales[locale{"ru", "RU"}] = struct{}{} | |||
return supportedLocales | |||
} | |||
func localExists(l locale) bool { | |||
_, exists := getLocales()[l] | |||
return exists | |||
} | |||
func main() { | |||
if len(os.Args) != 2 { | |||
fmt.Println("No locale passed") | |||
os.Exit(1) | |||
} | |||
localeParts := strings.Split(os.Args[1], "_") | |||
if len(localeParts) != 2 { | |||
fmt.Printf("Invalid locale passed: %v\n", os.Args[1]) | |||
os.Exit(1) | |||
} | |||
passedLocale := locale{ | |||
language: localeParts[0], | |||
region: localeParts[1], | |||
} | |||
if !localExists(passedLocale) { | |||
fmt.Printf("Locale not supported: %v\n", os.Args[1]) | |||
os.Exit(1) | |||
} | |||
fmt.Println("Locale passed is supported") | |||
} |
@ -0,0 +1,46 @@ | |||
package main | |||
import ( | |||
"fmt" | |||
"os" | |||
) | |||
func getPassedArgs(minArgs int) []string { | |||
if len(os.Args) < minArgs { | |||
fmt.Printf("At least %v arguments are needed\n", minArgs) | |||
os.Exit(1) | |||
} | |||
var args []string | |||
for i := 1; i < len(os.Args); i++ { | |||
args = append(args, os.Args[i]) | |||
} | |||
return args | |||
} | |||
func findLongest(args []string) string { | |||
var longest string | |||
for i := 0; i < len(args); i++ { | |||
if len(args[i]) > len(longest) { | |||
longest = args[i] | |||
} | |||
} | |||
return longest | |||
} | |||
func main() { | |||
if longest := findLongest(getPassedArgs(3)); len(longest) > 0 { | |||
fmt.Println("The longest word passed was:", longest) | |||
} else { | |||
fmt.Println("There was an error") | |||
os.Exit(1) | |||
} | |||
} |
@ -0,0 +1,33 @@ | |||
package main | |||
import ( | |||
"fmt" | |||
"os" | |||
) | |||
func getPassedArgs() []string { | |||
var args []string | |||
for i := 1; i < len(os.Args); i++ { | |||
args = append(args, os.Args[i]) | |||
} | |||
return args | |||
} | |||
func getLocals(extraLocals []string) []string { | |||
var locales []string | |||
locales = append(locales, "en_US", "fr_FR") | |||
locales = append(locales, extraLocals...) | |||
return locales | |||
} | |||
func main() { | |||
locales := getLocals(getPassedArgs()) | |||
fmt.Println("Locales to use:", locales) | |||
} |
@ -0,0 +1,21 @@ | |||
package main | |||
import "fmt" | |||
func message() string { | |||
s := []int{1, 2, 3, 4, 5, 6, 7, 8, 9} | |||
m := fmt.Sprintln("First: ", s[0], s[0:1], s[:1]) | |||
m += fmt.Sprintln("Last: ", s[len(s)-1], s[len(s)-1:len(s)], s[len(s)-1:]) | |||
m += fmt.Sprintln("First 5: ", s[:5]) | |||
m += fmt.Sprintln("Last 4: ", s[len(s)-4:]) | |||
m += fmt.Sprintln("Middle 5:", s[2:7]) | |||
return m | |||
} | |||
func main() { | |||
fmt.Print(message()) | |||
} |
@ -0,0 +1,21 @@ | |||
package main | |||
import "fmt" | |||
func genSlices() ([]int, []int, []int) { | |||
var s1 []int | |||
s2 := make([]int, 10) | |||
s3 := make([]int, 10, 50) | |||
return s1, s2, s3 | |||
} | |||
func main() { | |||
s1, s2, s3 := genSlices() | |||
fmt.Printf("s1: len = %v cap = %v\n", len(s1), cap(s1)) | |||
fmt.Printf("s2: len = %v cap = %v\n", len(s2), cap(s2)) | |||
fmt.Printf("s3: len = %v cap = %v\n", len(s3), cap(s3)) | |||
} |
@ -0,0 +1,82 @@ | |||
package main | |||
import "fmt" | |||
func linked() (int, int, int) { | |||
s1 := []int{1, 2, 3, 4, 5} | |||
s2 := s1 | |||
s3 := s1[:] | |||
s1[3] = 99 | |||
return s1[3], s2[3], s3[3] | |||
} | |||
func noLink() (int, int) { | |||
s1 := []int{1, 2, 3, 4, 5} | |||
s2 := s1 | |||
s1 = append(s1, 6) | |||
s1[3] = 99 | |||
return s1[3], s2[3] | |||
} | |||
func capLinked() (int, int) { | |||
s1 := make([]int, 5, 10) | |||
s1[0], s1[1], s1[2], s1[3], s1[4] = 1, 2, 3, 4, 5 | |||
s2 := s1 | |||
s1 = append(s1, 6) | |||
s1[3] = 99 | |||
return s1[3], s2[3] | |||
} | |||
func capNoLink() (int, int) { | |||
s1 := make([]int, 5, 10) | |||
s1[0], s1[1], s1[2], s1[3], s1[4] = 1, 2, 3, 4, 5 | |||
s2 := s1 | |||
s1 = append(s1, []int{10: 11}...) | |||
s1[3] = 99 | |||
return s1[3], s2[3] | |||
} | |||
func copyNoLink() (int, int, int) { | |||
s1 := []int{1, 2, 3, 4, 5} | |||
s2 := make([]int, len(s1)) | |||
copied := copy(s2, s1) | |||
s1[3] = 99 | |||
return s1[3], s2[3], copied | |||
} | |||
func appendNoLink() (int, int) { | |||
s1 := []int{1, 2, 3, 4, 5} | |||
s2 := append([]int{}, s1...) | |||
s1[3] = 99 | |||
return s1[3], s2[3] | |||
} | |||
func main() { | |||
l1, l2, l3 := linked() | |||
fmt.Println("Linked: ", l1, l2, l3) | |||
nl1, nl2 := noLink() | |||
fmt.Println("No Link: ", nl1, nl2) | |||
cl1, cl2 := capLinked() | |||
fmt.Println("Cap Linked: ", cl1, cl2) | |||
cn1, cn2 := capNoLink() | |||
fmt.Println("Cap no Link: ", cn1, cn2) | |||
copy1, copy2, copied := copyNoLink() | |||
fmt.Print("Copy No Link: ", copy1, copy2) | |||
fmt.Printf(" (Number of elements copied: %v\n", copied) | |||
a1, a2 := appendNoLink() | |||
fmt.Println("Append No Link: ", a1, a2) | |||
} |