Skip to content

Instantly share code, notes, and snippets.

@developerfred
Last active November 10, 2019 16:23
Show Gist options
  • Save developerfred/e45edd0c643400e4702e4fc20d10ef86 to your computer and use it in GitHub Desktop.
Save developerfred/e45edd0c643400e4702e4fc20d10ef86 to your computer and use it in GitHub Desktop.
Go BootCamp
package main
import "fmt"
func main() {
pow := make([]int, 10)
for i := range pow {
pow[i] = 1 << uint(i)
if pow[i] >= 16 {
break
}
}
fmt.Println(pow)
// [1 2 4 8 16 0 0 0 0 0]
}
package main
import (
"fmt"
)
func main() {
pow := make([]int, 10)
for i := range pow {
if i%2 == 0 {
continue
}
pow[i] = 1 << uint(i)
}
fmt.Println(pow)
//[0 2 0 8 0 32 0 128 0 512]
}
/* Give a list of names, you need to organize each name within a slice based on its length */
package main
import (
"fmt"
)
var names = []string{
"Katrina", "Evan", "Neil", "Adam", "Martin", "Matt", "Emma", "Isabella", "Emily", "Madison",
"Ava", "Olivia", "Sophie", "Abigail", "Elizabeth", "Chloe", "Samantha", "Addison", "Natalie", "Mia", "Alexis"}
func main() {
var maxLen int
for _, name := range names {
if l := len(name); l > maxLen {
maxLen = l
}
}
output := make([][]string, maxLen)
for _, name := range names {
output[len(name)-1] = append(
output[len(name)-1], name)
}
fmt.Printf("%v", output)
}
// [[] [] [Ava Mia] [Evan Neil Adam Matt Emma] [Emily Chloe] [Martin Olivia Sophie Alexis] [Katrina Madison Abigail Addison Natalie] [Isabella Samantha] [Elizabeth]]
package main
import (
"fmt"
)
func main() {
celebs := map[string]int{
"Nicolas Cage": 50,
"Selena Gomes": 21,
"Jude Law": 41,
"Scarlett Johansson": 29,
}
fmt.Printf("%#v", celebs)
}
package main
import (
"fmt"
)
func main() {
pow := make([]int, 10)
for i := range pow {
pow[i] = 1 << uint(i)
}
for _, value := range pow {
fmt.Printf("%d\n", value)
}
}
1
2
4
8
16
32
64
128
256
512
package main
import (
"fmt"
)
func main() {
cities := map[string]int{
"New York": 8336697,
"Los Angeles": 3857799,
"Chicago": 2714856,
}
for key, value := range cities {
fmt.Printf("%s has %d inhabitants\n", key, value)
}
}
// Chicago has 2714856 inhabitants
// New York has 8336697 inhabitants
// Los Angeles has 3857799 inhabitants
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment