refactor: x-tba

This commit is contained in:
Inanc Gumus
2019-04-12 11:58:12 +03:00
parent e591eb0d45
commit a5fad24374
35 changed files with 114 additions and 337 deletions

View File

@ -12,6 +12,7 @@ import (
"strings"
)
// named result values
func parse(data string) (props []Property) {
rows := strings.Split(data, "\n")

View File

@ -0,0 +1,65 @@
// For more tutorials: https://blog.learngoprogramming.com
//
// Copyright © 2018 Inanc Gumus
// Learn Go Programming Course
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
//
package main
import (
"fmt"
)
func main() {
// As you've learned before Go is a pass-by-value programming language. So, when you assign a variable to another one, or pass it to a function, it gets copied.
// #1: Let's take a look at an example.
// Let's say you have an int variable.
// You want a function to increase the number.
number := 1
// In that case, you need to use a pointer because a pointer can send the memory location of the variable to the function, so the function can change the value directly on the memory.
//
// For example, let me pass the memory address of the number to the function like so.
//
// Remember: Ampersand sign here returns the memory location of the given variable.
incr(&number)
// Let me print the variable.
fmt.Println("&main.number :", number)
// #1C: Let's check it out.
// As you can see, now the number has changed to 2.
// The function could increment the number because it can directly access to the value of the variable by using its memory address.
// If you look at the memory address of the number variable, you will see that the function sees the same address, let me show you.
//
// Let me print the address of the number variable like so.
// Every pointer stores a memory location value.
// So the %p verb prints the address that a pointer stores.
fmt.Printf("&main.number : %p\n", &number)
}
// #1B: Let me also declare the function like so.
// This time, it accepts an int pointer.
// When you put an asterisk sign in front of a type, it becomes a pointer.
// Every type comes with its own pointer type like so.
// This is an int pointer because it has an asterisk in front of the int type. So, this pointer can only store the memory address of an int value.
func incr(num *int) {
// Inside the function I'm going to change the num variable by deferencing it like so, then I'm going to increase it as usual.
*num++
// #1D: Let me also print the memory address that the num pointer stores.
// Remember: num here is a pointer variable, it stores a memory address of an int value.
fmt.Printf("incr.number : %p\n", num)
// As you can see, the main function and the incr function use the same variable. They both are looking at the same memory location. That's why this function can change the value.
// However, if I print the memory address of the local num variable, it will print a different address, let me show you.
fmt.Printf("&incr.number : %p\n", &num)
// As you can see, the memory address of the local num variable is different than the memory address of this variable. It's because, Go is a pass-by-value language. So, it even copies the pointer variables. So, the local num pointer variable is a new variable inside this function. However, it stores the same address of the number variable of main function.
// In Go, everything is passed by value, even pointers, however, when the functions knows the memory address of a variable, it can change it directly. Otherwise it cannot change it because the variable belongs to the local scope of the function.
}

View File

@ -0,0 +1,12 @@
// For more tutorials: https://blog.learngoprogramming.com
//
// Copyright © 2018 Inanc Gumus
// Learn Go Programming Course
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
//
package main
func main() {
}

View File

@ -0,0 +1,12 @@
// For more tutorials: https://blog.learngoprogramming.com
//
// Copyright © 2018 Inanc Gumus
// Learn Go Programming Course
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
//
package main
func main() {
}

View File

@ -0,0 +1,12 @@
// For more tutorials: https://blog.learngoprogramming.com
//
// Copyright © 2018 Inanc Gumus
// Learn Go Programming Course
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
//
package main
func main() {
}

View File

@ -0,0 +1,12 @@
// For more tutorials: https://blog.learngoprogramming.com
//
// Copyright © 2018 Inanc Gumus
// Learn Go Programming Course
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
//
package main
func main() {
}

View File

@ -1,35 +0,0 @@
// For more tutorials: https://blog.learngoprogramming.com
//
// Copyright © 2018 Inanc Gumus
// Learn Go Programming Course
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
//
package main
import (
"fmt"
"os"
)
func main() {
args := os.Args[1:]
if len(args) != 1 {
fmt.Println("[english word] -> [turkish word]")
return
}
query := args[0]
english := []string{"good", "great", "perfect"}
turkish := []string{"iyi", "harika", "mükemmel"}
// O(n) -> Inefficient: Depends on 'n'.
for i, w := range english {
if query == w {
fmt.Printf("%q means %q\n", w, turkish[i])
return
}
}
fmt.Printf("%q not found\n", query)
}

View File

@ -1,52 +0,0 @@
// For more tutorials: https://blog.learngoprogramming.com
//
// Copyright © 2018 Inanc Gumus
// Learn Go Programming Course
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
//
package main
import (
"fmt"
)
func main() {
/*
#1: Nil Map: Read-Only
*/
var dict map[string]string
fmt.Printf("Zero-value of a map: %#v\n", dict)
/*
#5: You cannot assign to a nil map.
*/
// dict["up"] = "yukarı"
// dict["down"] = "aşağı"
/*
#2: Map retrieval is O(1) — on average.
*/
key := "good"
//
// #4: you can use an uninitialized map without checking it is nil
//
// if dict != nil {
value := dict[key]
fmt.Printf("%q means %#v\n", key, value)
// }
/*
#3: Cannot use non-comparable types as map key types
*/
// var broken map[[]int]int
// var broken map[map[int]string]bool
//
// A map can only be compared to nil value
// _ = dict == nil
/*
#1 Step 2
*/
fmt.Printf("The dictionary contains %d words.\n", len(dict))
}

View File

@ -1,119 +0,0 @@
// For more tutorials: https://blog.learngoprogramming.com
//
// Copyright © 2018 Inanc Gumus
// Learn Go Programming Course
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
//
package main
import (
"fmt"
"os"
)
func main() {
/*
#2—A: Get the key from CLI
*/
args := os.Args[1:]
if len(args) != 1 {
fmt.Println("[english word] -> [turkish word]")
return
}
query := args[0]
/*
#1: Empty Map Literal
*/
// dict := map[string]string{}
/*
#3: Map Literal
Creates, initializes and returns a new map
with the given key-value pairs
*/
dict := map[string]string{
"good": "kötü",
"great": "harika",
"perfect": "mükemmel",
/*
#4: keys and values should be the same type
(key and value types do not need to be the same)
*/
// 42: "forty two",
// "forty two": 42,
}
// you can only add new pairs to an initialized map
dict["up"] = "yukarı" // adds a new pair
dict["down"] = "aşağı" // adds a new pair
dict["good"] = "iyi" // #5: overwrites the value at the key: "good"
dict["mistake"] = "" // #8: a key with a zero-value
// you can query an uninitialized map for its length
fmt.Printf("The dictionary contains %d words.\n", len(dict))
/*
#6: getting values from a map using keys directly
*/
// fmt.Println("good -> ", dict["good"])
// fmt.Println("great -> ", dict["great"])
// fmt.Println("perfect -> ", dict["perfect"])
/*
#11: looping over a map might be a sign of a design mistake
in your software.
(it prints unordered output)
*/
// for k, v := range dict {
// fmt.Printf("%q means %#v\n", k, v)
// }
/*
#12: printing a map (ordered output since Go 1.12)
*/
// fmt.Printf("%#v\n", dict)
/*
#13: compare a map using its printed output
(handy when testing)
*/
// copied := map[string]string{"up": "yukarı", "down": "aşağı",
// "mistake": "", "good": "iyi", "great": "harika",
// "perfect": "mükemmel"}
// first := fmt.Sprintf("%s", dict)
// second := fmt.Sprintf("%s", copied)
// if first == second {
// fmt.Println("Maps are equal")
// }
/*
#2—B: retrieve values by key - O(1) efficiency
(even from an uninitialized map)
*/
// value := dict[query]
/*
#7: check for non-existing key using zero-value
*/
// if value == "" {}
/*
#9: check for non-existing key: with comma, ok
*/
// value, ok := dict[query]
// if !ok {}
/*
#10: comma ok in a short if
*/
if value, ok := dict[query]; ok {
fmt.Printf("%q means %#v\n", query, value)
return
}
fmt.Printf("%q not found.\n", query)
}

View File

@ -1,77 +0,0 @@
// For more tutorials: https://blog.learngoprogramming.com
//
// Copyright © 2018 Inanc Gumus
// Learn Go Programming Course
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
//
package main
import (
"fmt"
"os"
)
func main() {
args := os.Args[1:]
if len(args) != 1 {
fmt.Println("[english word] -> [turkish word]")
return
}
query := args[0]
dict := map[string]string{
"good": "iyi",
"great": "harika",
"perfect": "mükemmel",
// #5: this overwrites the mükemmel in the turkish map
"awesome": "mükemmel",
}
// #1: map value is a pointer to a real map in the memory
// turkish := dict
// turkish["good"] = "güzel"
// dict["great"] = "kusursuz"
// #6: delete can delete a pair from a map
delete(dict, "awesome")
// #7: no-op
delete(dict, "awesome")
delete(dict, "notexisting")
// #8: destroying a map completely (false)
// dict = nil
// #9: destroying a map completely (true)
// replaces the whole loop with a single instruction:
// "call runtime.mapclear()"
for k := range dict {
delete(dict, k)
}
// #2: make initializes a new map value and returns a pointer to it
// turkish := make(map[string]string)
// #3: make(T, hint) => hint is an advice for initing a large enough map
turkish := make(map[string]string, len(dict))
for k, v := range dict {
turkish[v] = k
}
// #4: add turkish dictionary
// fmt.Printf("english: %q\nturkish: %q\n", dict, turkish)
if value, ok := dict[query]; ok {
fmt.Printf("%q means %#v\n", query, value)
return
}
if value, ok := turkish[query]; ok {
fmt.Printf("%q means %#v\n", query, value)
return
}
fmt.Printf("%q not found.\n", query)
}

View File

@ -1,49 +0,0 @@
// For more tutorials: https://blog.learngoprogramming.com
//
// Copyright © 2018 Inanc Gumus
// Learn Go Programming Course
// License: https://creativecommons.org/licenses/by-nc-sa/4.0/
//
package main
import "fmt"
func main() {
// set is a data structure that contain the same element only once.
// maps can also be used as sets.
// let's create a slice of coders.
coders := []string{"pike", "thompson", "lovelace", "knuth"}
// let's create a map for tracking whether they are gopher or not.
gophers := make(map[string]bool, len(coders))
// let's mark the gophers
gophers["thompson"] = true
gophers["pike"] = true
// print the gophers
for _, coder := range coders {
// i see this a lot, especially in beginner code.
//
// you don't have to use the "comma, ok" idiom.
// m[k] = false for non-existing bool elements.
// use it to your advantage.
//
// DONT:
// if _, ok := gophers[coder]; ok { ... }
//
// DO:
// for this trick to work, you should set all the map elements to true
if gophers[coder] {
fmt.Printf("%s is a gopher.\n", coder)
// don't use else. stay at the same indentation level.
continue
}
fmt.Printf("%s is not a gopher.\n", coder)
}
}

View File

@ -1,5 +0,0 @@
# IDEAS
* Retrieve sorted map keys
* Roman literals