massive: move a lot of things
This commit is contained in:
26
25-functions-and-pointers/01-basics/bad.go
Normal file
26
25-functions-and-pointers/01-basics/bad.go
Normal file
@@ -0,0 +1,26 @@
|
||||
// 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 showN() {
|
||||
if N == 0 {
|
||||
fmt.Println("showN : N is zero, increment it.")
|
||||
return
|
||||
}
|
||||
fmt.Printf("showN : N = %d\n", N)
|
||||
}
|
||||
|
||||
func incrN() {
|
||||
N++
|
||||
}
|
||||
|
||||
// you cannot declare a function within the same package with the same name
|
||||
// func incrN() {
|
||||
// }
|
||||
33
25-functions-and-pointers/01-basics/main.go
Normal file
33
25-functions-and-pointers/01-basics/main.go
Normal file
@@ -0,0 +1,33 @@
|
||||
// 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/
|
||||
//
|
||||
|
||||
//
|
||||
// You need run this program like so:
|
||||
// go run .
|
||||
//
|
||||
// This will magically pass all the go files in the current directory to the
|
||||
// Go compiler.
|
||||
//
|
||||
//
|
||||
// BUT NOT like so:
|
||||
// go run main.go
|
||||
//
|
||||
// Because, the compiler needs to see global.go too
|
||||
// It can't magically find global.go — what you give is what you get.
|
||||
//
|
||||
|
||||
package main
|
||||
|
||||
// N is a shared counter which is BAD
|
||||
var N int
|
||||
|
||||
func main() {
|
||||
showN()
|
||||
incrN()
|
||||
incrN()
|
||||
showN()
|
||||
}
|
||||
117
25-functions-and-pointers/02-basics/main.go
Normal file
117
25-functions-and-pointers/02-basics/main.go
Normal file
@@ -0,0 +1,117 @@
|
||||
// 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/
|
||||
//
|
||||
|
||||
//
|
||||
// You need run this program like so:
|
||||
// go run .
|
||||
//
|
||||
// This will magically pass all the go files in the current directory to the
|
||||
// Go compiler.
|
||||
//
|
||||
//
|
||||
// BUT NOT like so:
|
||||
// go run main.go
|
||||
//
|
||||
// Because, the compiler needs to see global.go too
|
||||
// It can't magically find global.go — what you give is what you get.
|
||||
//
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
func main() {
|
||||
local := 10
|
||||
show(local)
|
||||
|
||||
incrWrong(local)
|
||||
fmt.Printf("local → %d\n", local)
|
||||
|
||||
// incr(local)
|
||||
local = incr(local)
|
||||
show(local)
|
||||
|
||||
local = incrBy(local, 5)
|
||||
show(local)
|
||||
|
||||
_, err := incrByStr(local, "TWO")
|
||||
if err != nil {
|
||||
fmt.Printf("err → %s\n", err)
|
||||
}
|
||||
|
||||
local, _ = incrByStr(local, "2")
|
||||
show(local)
|
||||
|
||||
// CHAINING
|
||||
|
||||
// can't save the number back into main's local
|
||||
show(incrBy(local, 2))
|
||||
show(local)
|
||||
|
||||
// can't pass the results of the multiple-value returning func
|
||||
// show(incrByStr(local, "3"))
|
||||
|
||||
// can call showErr directly because it accepts the same types
|
||||
// of parameters as with incrByStr's result values.
|
||||
// local = sanitize(incrByStr(local, "NOPE"))
|
||||
// show(local)
|
||||
|
||||
local = sanitize(incrByStr(local, "2"))
|
||||
show(local)
|
||||
|
||||
local = limit(incrBy(local, 5), 2000)
|
||||
show(local)
|
||||
}
|
||||
|
||||
func show(n int) {
|
||||
// can't access main's local
|
||||
// fmt.Printf("show : n = %d\n", local)
|
||||
fmt.Printf("show → n = %d\n", n)
|
||||
}
|
||||
|
||||
// wrong: incr can't access to main's `local`
|
||||
func incrWrong(n int) {
|
||||
// n := local
|
||||
n++
|
||||
// can't return (there are no result values)
|
||||
// return n
|
||||
}
|
||||
|
||||
func incr(n int) int {
|
||||
n++
|
||||
return n
|
||||
}
|
||||
|
||||
func incrBy(n, factor int) int {
|
||||
return n * factor
|
||||
}
|
||||
|
||||
func incrByStr(n int, factor string) (int, error) {
|
||||
m, err := strconv.Atoi(factor)
|
||||
n = incrBy(n, m)
|
||||
return n, err
|
||||
}
|
||||
|
||||
func sanitize(n int, err error) int {
|
||||
if err != nil {
|
||||
return 0
|
||||
}
|
||||
return n
|
||||
}
|
||||
|
||||
func limit(n, lim int) (m int) {
|
||||
// var m int
|
||||
m = n
|
||||
if m >= lim {
|
||||
m = lim
|
||||
}
|
||||
// return m
|
||||
return
|
||||
}
|
||||
6
25-functions-and-pointers/03-refactor-to-funcs/log.txt
Normal file
6
25-functions-and-pointers/03-refactor-to-funcs/log.txt
Normal file
@@ -0,0 +1,6 @@
|
||||
learngoprogramming.com 10
|
||||
learngoprogramming.com 10
|
||||
golang.org 4
|
||||
golang.org 6
|
||||
blog.golang.org 20
|
||||
blog.golang.org 10
|
||||
@@ -0,0 +1,6 @@
|
||||
learngoprogramming.com 10
|
||||
learngoprogramming.com 10
|
||||
golang.org
|
||||
golang.org 6
|
||||
blog.golang.org 20
|
||||
blog.golang.org 10
|
||||
@@ -0,0 +1,6 @@
|
||||
learngoprogramming.com 10
|
||||
learngoprogramming.com 10
|
||||
golang.org -100
|
||||
golang.org 6
|
||||
blog.golang.org 20
|
||||
blog.golang.org 10
|
||||
@@ -0,0 +1,6 @@
|
||||
learngoprogramming.com 10
|
||||
learngoprogramming.com 10
|
||||
golang.org FOUR
|
||||
golang.org 6
|
||||
blog.golang.org 20
|
||||
blog.golang.org 10
|
||||
62
25-functions-and-pointers/03-refactor-to-funcs/main.go
Normal file
62
25-functions-and-pointers/03-refactor-to-funcs/main.go
Normal file
@@ -0,0 +1,62 @@
|
||||
// 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 (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"os"
|
||||
"sort"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func main() {
|
||||
p := newParser()
|
||||
|
||||
// Scan the standard-in line by line
|
||||
in := bufio.NewScanner(os.Stdin)
|
||||
for in.Scan() {
|
||||
p.lines++
|
||||
|
||||
parsed, err := parse(p, in.Text())
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
|
||||
domain, visits := parsed.domain, parsed.visits
|
||||
|
||||
// Collect the unique domains
|
||||
if _, ok := p.sum[domain]; !ok {
|
||||
p.domains = append(p.domains, domain)
|
||||
}
|
||||
|
||||
// Keep track of total and per domain visits
|
||||
p.total += visits
|
||||
|
||||
// create and assign a new copy of `visit`
|
||||
p.sum[domain] = result{
|
||||
domain: domain,
|
||||
visits: visits + p.sum[domain].visits,
|
||||
}
|
||||
}
|
||||
|
||||
// Print the visits per domain
|
||||
sort.Strings(p.domains)
|
||||
|
||||
fmt.Printf("%-30s %10s\n", "DOMAIN", "VISITS")
|
||||
fmt.Println(strings.Repeat("-", 45))
|
||||
|
||||
for _, domain := range p.domains {
|
||||
parsed := p.sum[domain]
|
||||
fmt.Printf("%-30s %10d\n", domain, parsed.visits)
|
||||
}
|
||||
|
||||
// Print the total visits for all domains
|
||||
fmt.Printf("\n%-30s %10d\n", "TOTAL", p.total)
|
||||
}
|
||||
53
25-functions-and-pointers/03-refactor-to-funcs/parser.go
Normal file
53
25-functions-and-pointers/03-refactor-to-funcs/parser.go
Normal file
@@ -0,0 +1,53 @@
|
||||
// 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"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// result stores the parsed result for a domain
|
||||
type result struct {
|
||||
domain string
|
||||
visits int
|
||||
// add more metrics if needed
|
||||
}
|
||||
|
||||
// parser keep tracks of the parsing
|
||||
type parser struct {
|
||||
sum map[string]result // metrics per domain
|
||||
domains []string // unique domain names
|
||||
total int // total visits for all domains
|
||||
lines int // number of parsed lines (for the error messages)
|
||||
}
|
||||
|
||||
// newParser constructs, initializes and returns a new parser
|
||||
func newParser() parser {
|
||||
return parser{sum: make(map[string]result)}
|
||||
}
|
||||
|
||||
// parse parses a log line and returns the parsed result with an error
|
||||
func parse(p parser, line string) (parsed result, err error) {
|
||||
fields := strings.Fields(line)
|
||||
if len(fields) != 2 {
|
||||
err = fmt.Errorf("wrong input: %v (line #%d)", fields, p.lines)
|
||||
return
|
||||
}
|
||||
|
||||
parsed.domain = fields[0]
|
||||
|
||||
parsed.visits, err = strconv.Atoi(fields[1])
|
||||
if parsed.visits < 0 || err != nil {
|
||||
err = fmt.Errorf("wrong input: %q (line #%d)", fields[1], p.lines)
|
||||
return
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
learngoprogramming.com 10
|
||||
learngoprogramming.com 10
|
||||
golang.org 4
|
||||
golang.org 6
|
||||
blog.golang.org 20
|
||||
blog.golang.org 10
|
||||
@@ -0,0 +1,6 @@
|
||||
learngoprogramming.com 10
|
||||
learngoprogramming.com 10
|
||||
golang.org
|
||||
golang.org 6
|
||||
blog.golang.org 20
|
||||
blog.golang.org 10
|
||||
@@ -0,0 +1,6 @@
|
||||
learngoprogramming.com 10
|
||||
learngoprogramming.com 10
|
||||
golang.org -100
|
||||
golang.org 6
|
||||
blog.golang.org 20
|
||||
blog.golang.org 10
|
||||
@@ -0,0 +1,6 @@
|
||||
learngoprogramming.com 10
|
||||
learngoprogramming.com 10
|
||||
golang.org FOUR
|
||||
golang.org 6
|
||||
blog.golang.org 20
|
||||
blog.golang.org 10
|
||||
48
25-functions-and-pointers/04-pass-by-value-semantics/main.go
Normal file
48
25-functions-and-pointers/04-pass-by-value-semantics/main.go
Normal file
@@ -0,0 +1,48 @@
|
||||
// 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 (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"os"
|
||||
"sort"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func main() {
|
||||
p := newParser()
|
||||
|
||||
// Scan the standard-in line by line
|
||||
in := bufio.NewScanner(os.Stdin)
|
||||
for in.Scan() {
|
||||
p.lines++
|
||||
|
||||
parsed, err := parse(p, in.Text())
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
break
|
||||
}
|
||||
|
||||
p = update(p, parsed)
|
||||
}
|
||||
|
||||
// Print the visits per domain
|
||||
sort.Strings(p.domains)
|
||||
|
||||
fmt.Printf("%-30s %10s\n", "DOMAIN", "VISITS")
|
||||
fmt.Println(strings.Repeat("-", 45))
|
||||
|
||||
for _, domain := range p.domains {
|
||||
parsed := p.sum[domain]
|
||||
fmt.Printf("%-30s %10d\n", domain, parsed.visits)
|
||||
}
|
||||
|
||||
// Print the total visits for all domains
|
||||
fmt.Printf("\n%-30s %10d\n", "TOTAL", p.total)
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
// 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"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// result stores metrics for a domain
|
||||
type result struct {
|
||||
domain string
|
||||
visits int
|
||||
// add more metrics if needed
|
||||
}
|
||||
|
||||
// parser keep tracks of the parsing
|
||||
type parser struct {
|
||||
sum map[string]result // metrics per domain
|
||||
domains []string // unique domain names
|
||||
total int // total visits for all domains
|
||||
lines int // number of parsed lines (for the error messages)
|
||||
}
|
||||
|
||||
func newParser() parser {
|
||||
return parser{sum: make(map[string]result)}
|
||||
}
|
||||
|
||||
func parse(p parser, line string) (parsed result, err error) {
|
||||
fields := strings.Fields(line)
|
||||
if len(fields) != 2 {
|
||||
err = fmt.Errorf("wrong input: %v (line #%d)", fields, p.lines)
|
||||
return
|
||||
}
|
||||
|
||||
parsed.domain = fields[0]
|
||||
|
||||
parsed.visits, err = strconv.Atoi(fields[1])
|
||||
if parsed.visits < 0 || err != nil {
|
||||
err = fmt.Errorf("wrong input: %q (line #%d)", fields[1], p.lines)
|
||||
return
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func update(p parser, parsed result) parser {
|
||||
domain, visits := parsed.domain, parsed.visits
|
||||
|
||||
// Collect the unique domains
|
||||
if _, ok := p.sum[domain]; !ok {
|
||||
p.domains = append(p.domains, domain)
|
||||
}
|
||||
|
||||
// Keep track of total and per domain visits
|
||||
p.total += visits
|
||||
|
||||
// create and assign a new copy of `visit`
|
||||
p.sum[domain] = result{
|
||||
domain: domain,
|
||||
visits: visits + p.sum[domain].visits,
|
||||
}
|
||||
|
||||
return p
|
||||
}
|
||||
39
25-functions-and-pointers/05-pointers/main.go
Normal file
39
25-functions-and-pointers/05-pointers/main.go
Normal file
@@ -0,0 +1,39 @@
|
||||
// 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() {
|
||||
var counter byte = 100
|
||||
P := &counter
|
||||
V := *P
|
||||
|
||||
fmt.Printf("counter : %-16d address: %-16p\n", counter, &counter)
|
||||
fmt.Printf("P : %-16p address: %-16p *P: %-16d\n", P, &P, *P)
|
||||
fmt.Printf("V : %-16d address: %-16p\n", V, &V)
|
||||
|
||||
V = 200
|
||||
fmt.Println()
|
||||
fmt.Printf("counter : %-16d address: %-16p\n", counter, &counter)
|
||||
fmt.Printf("P : %-16p address: %-16p *P: %-16d\n", P, &P, *P)
|
||||
fmt.Printf("V : %-16d address: %-16p\n", V, &V)
|
||||
|
||||
V = counter // reset the V to counter's initial value
|
||||
counter++
|
||||
fmt.Println()
|
||||
fmt.Printf("counter : %-16d address: %-16p\n", counter, &counter)
|
||||
fmt.Printf("P : %-16p address: %-16p *P: %-16d\n", P, &P, *P)
|
||||
fmt.Printf("V : %-16d address: %-16p\n", V, &V)
|
||||
|
||||
*P = 25
|
||||
fmt.Println()
|
||||
fmt.Printf("counter : %-16d address: %-16p\n", counter, &counter)
|
||||
fmt.Printf("P : %-16p address: %-16p *P: %-16d\n", P, &P, *P)
|
||||
fmt.Printf("V : %-16d address: %-16p\n", V, &V)
|
||||
}
|
||||
63
25-functions-and-pointers/06-pointers-basic-examples/main.go
Normal file
63
25-functions-and-pointers/06-pointers-basic-examples/main.go
Normal file
@@ -0,0 +1,63 @@
|
||||
// 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() {
|
||||
var (
|
||||
counter int
|
||||
V int
|
||||
P *int
|
||||
)
|
||||
|
||||
if P == nil {
|
||||
fmt.Printf("P is nil and its address is %p\n", P)
|
||||
}
|
||||
|
||||
counter = 100 // counter is an int variable
|
||||
P = &counter // P is a pointer int variable
|
||||
V = *P // V is a int variable (a copy of counter)
|
||||
|
||||
if P == &counter {
|
||||
fmt.Printf("P's address is equal to counter: %p == %p\n", P, &counter)
|
||||
}
|
||||
|
||||
fmt.Printf("counter : %-16d address: %-16p\n", counter, &counter)
|
||||
fmt.Printf("P : %-16p address: %-16p *P: %-16d\n", P, &P, *P)
|
||||
fmt.Printf("V : %-16d address: %-16p\n", V, &V)
|
||||
|
||||
fmt.Println("\n••••• CHANGE: counter")
|
||||
counter = 10 // V doesn't change because it's a copy
|
||||
fmt.Printf("counter : %-16d address: %-16p\n", counter, &counter)
|
||||
fmt.Printf("V : %-16d address: %-16p\n", V, &V)
|
||||
|
||||
fmt.Println("\n••••• CHANGE IN: passVal")
|
||||
counter = passVal(counter)
|
||||
fmt.Printf("counter : %-16d address: %-16p\n", counter, &counter)
|
||||
|
||||
fmt.Println("\n••••• CHANGE IN: passPtrVal")
|
||||
passPtrVal(&counter) // same as passPtrVal(&counter) (no need to return)
|
||||
fmt.Printf("counter : %-16d address: %-16p\n", counter, &counter)
|
||||
}
|
||||
|
||||
// *pn is a int pointer variable (copy of P)
|
||||
func passPtrVal(pn *int) {
|
||||
fmt.Printf("pn : %-16p address: %-16p *pn: %d\n", pn, &pn, *pn)
|
||||
|
||||
// pointers can breach function isolation borders
|
||||
*pn++ // counter changes because `pn` points to `counter` — (*pn)++
|
||||
fmt.Printf("pn : %-16p address: %-16p *pn: %d\n", pn, &pn, *pn)
|
||||
}
|
||||
|
||||
// n is a int variable (copy of counter)
|
||||
func passVal(n int) int {
|
||||
n = 50 // counter doesn't change because `n` is a copy
|
||||
fmt.Printf("n : %-16d address: %-16p\n", n, &n)
|
||||
return n
|
||||
}
|
||||
141
25-functions-and-pointers/07-pointers-composites/main.go
Normal file
141
25-functions-and-pointers/07-pointers-composites/main.go
Normal file
@@ -0,0 +1,141 @@
|
||||
// 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"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func main() {
|
||||
fmt.Println("••••• ARRAYS")
|
||||
arrays()
|
||||
|
||||
fmt.Println("\n••••• SLICES")
|
||||
slices()
|
||||
|
||||
fmt.Println("\n••••• MAPS")
|
||||
maps()
|
||||
|
||||
fmt.Println("\n••••• STRUCTS")
|
||||
structs()
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
type house struct {
|
||||
name string
|
||||
rooms int
|
||||
}
|
||||
|
||||
func structs() {
|
||||
myHouse := house{name: "My House", rooms: 5}
|
||||
|
||||
addRoom(myHouse)
|
||||
|
||||
// fmt.Printf("%+v\n", myHouse)
|
||||
fmt.Printf("structs() : %p %+v\n", &myHouse, myHouse)
|
||||
|
||||
addRoomPtr(&myHouse)
|
||||
fmt.Printf("structs() : %p %+v\n", &myHouse, myHouse)
|
||||
|
||||
fmt.Printf("&myHouse.name : %p\n", &myHouse.name)
|
||||
fmt.Printf("&myHouse.rooms: %p\n", &myHouse.rooms)
|
||||
}
|
||||
|
||||
func addRoom(h house) {
|
||||
h.rooms++
|
||||
fmt.Printf("addRoom() : %p %+v\n", &h, h)
|
||||
}
|
||||
|
||||
func addRoomPtr(h *house) {
|
||||
// (*h).rooms++
|
||||
h.rooms++
|
||||
fmt.Printf("addRoomPtr() : %p %+v\n", h, h)
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
func maps() {
|
||||
confused := map[string]int{"one": 2, "two": 1}
|
||||
|
||||
fix(confused)
|
||||
|
||||
fmt.Println(confused)
|
||||
|
||||
// &confused["one"]
|
||||
}
|
||||
|
||||
func fix(m map[string]int) {
|
||||
m["one"] = 1
|
||||
m["two"] = 2
|
||||
m["three"] = 3
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
func slices() {
|
||||
dirs := []string{"up", "down", "left", "right"}
|
||||
|
||||
up(dirs)
|
||||
fmt.Printf("slices list : %p %q\n", &dirs, dirs)
|
||||
|
||||
upPtr(&dirs)
|
||||
fmt.Printf("slices list : %p %q\n", &dirs, dirs)
|
||||
}
|
||||
|
||||
func up(list []string) {
|
||||
for i := range list {
|
||||
list[i] = strings.ToUpper(list[i])
|
||||
fmt.Printf("up.list[%d] : %p\n", i, &list[i])
|
||||
}
|
||||
// *list = append(*list, "HEISEN BUG")
|
||||
list = append(list, "HEISEN BUG")
|
||||
|
||||
fmt.Printf("up list : %p %q\n", &list, list)
|
||||
}
|
||||
|
||||
func upPtr(list *[]string) {
|
||||
lv := *list
|
||||
|
||||
for i := range lv {
|
||||
lv[i] = strings.ToUpper(lv[i])
|
||||
}
|
||||
*list = append(*list, "HEISEN BUG")
|
||||
|
||||
fmt.Printf("upPtr list : %p %q\n", list, list)
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
func arrays() {
|
||||
nums := [...]int{1, 2, 3}
|
||||
|
||||
incr(nums)
|
||||
fmt.Printf("arrays nums : %p\n", &nums)
|
||||
fmt.Println(nums)
|
||||
|
||||
incrByPtr(&nums)
|
||||
fmt.Println(nums)
|
||||
}
|
||||
|
||||
func incr(nums [3]int) {
|
||||
fmt.Printf("incr nums : %p\n", &nums)
|
||||
|
||||
for i := range nums {
|
||||
nums[i]++
|
||||
fmt.Printf("incr.nums[%d] : %p\n", i, &nums[i])
|
||||
}
|
||||
}
|
||||
|
||||
func incrByPtr(nums *[3]int) {
|
||||
for i := range nums {
|
||||
nums[i]++
|
||||
// (*nums)[i]++
|
||||
}
|
||||
}
|
||||
6
25-functions-and-pointers/08-log-parser-pointers/log.txt
Normal file
6
25-functions-and-pointers/08-log-parser-pointers/log.txt
Normal file
@@ -0,0 +1,6 @@
|
||||
learngoprogramming.com 10
|
||||
learngoprogramming.com 10
|
||||
golang.org 4
|
||||
golang.org 6
|
||||
blog.golang.org 20
|
||||
blog.golang.org 10
|
||||
@@ -0,0 +1,6 @@
|
||||
learngoprogramming.com 10
|
||||
learngoprogramming.com 10
|
||||
golang.org
|
||||
golang.org 6
|
||||
blog.golang.org 20
|
||||
blog.golang.org 10
|
||||
@@ -0,0 +1,6 @@
|
||||
learngoprogramming.com 10
|
||||
learngoprogramming.com 10
|
||||
golang.org -100
|
||||
golang.org 6
|
||||
blog.golang.org 20
|
||||
blog.golang.org 10
|
||||
@@ -0,0 +1,6 @@
|
||||
learngoprogramming.com 10
|
||||
learngoprogramming.com 10
|
||||
golang.org FOUR
|
||||
golang.org 6
|
||||
blog.golang.org 20
|
||||
blog.golang.org 10
|
||||
36
25-functions-and-pointers/08-log-parser-pointers/main.go
Normal file
36
25-functions-and-pointers/08-log-parser-pointers/main.go
Normal file
@@ -0,0 +1,36 @@
|
||||
// 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 (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"os"
|
||||
)
|
||||
|
||||
func main() {
|
||||
in := bufio.NewScanner(os.Stdin)
|
||||
|
||||
p := newParser()
|
||||
|
||||
for in.Scan() {
|
||||
add(p, in.Text())
|
||||
}
|
||||
|
||||
for _, name := range p.domains {
|
||||
// vis := p.sum[d.name]
|
||||
d := p.sum[name]
|
||||
|
||||
fmt.Printf("%-25s -> %d\n", d.name, d.visits)
|
||||
}
|
||||
fmt.Printf("\n%-25s -> %d\n", "TOTAL", p.total)
|
||||
|
||||
if p.lerr != nil {
|
||||
fmt.Printf("> Err: %s\n", p.lerr)
|
||||
}
|
||||
}
|
||||
99
25-functions-and-pointers/08-log-parser-pointers/parser.go
Normal file
99
25-functions-and-pointers/08-log-parser-pointers/parser.go
Normal file
@@ -0,0 +1,99 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// TODO: add add() func
|
||||
// TODO: add error handling (variadics)
|
||||
// TODO: add iterator func values
|
||||
// TODO: add summarizer to main()
|
||||
|
||||
// domain represents a single domain log record
|
||||
type domain struct {
|
||||
name string
|
||||
visits int
|
||||
}
|
||||
|
||||
// parser parses a log file and provides an iterator to iterate upon the domains
|
||||
//
|
||||
// the parser struct is carefully crafted to be usable using its zero values except the map field
|
||||
type parser struct {
|
||||
// sum map[string]int // visits per unique domain
|
||||
// domains []domain // unique domain names
|
||||
sum map[string]domain // visits per unique domain
|
||||
domains []string // unique domain names
|
||||
|
||||
total int // total visits to all domains
|
||||
lines int // number of parsed lines (for the error messages)
|
||||
lerr error // saves the last error occurred
|
||||
}
|
||||
|
||||
// newParser creates and returns a new parser.
|
||||
func newParser() *parser {
|
||||
// return &parser{sum: make(map[string]int)}
|
||||
return &parser{sum: make(map[string]domain)}
|
||||
}
|
||||
|
||||
// add parses the given line and saves the result to the internal list of
|
||||
// domains. it doesn't add the record when the parsing fails.
|
||||
func add(p *parser, line string) {
|
||||
// if there was a previous error do not add
|
||||
if p.lerr != nil {
|
||||
return
|
||||
}
|
||||
|
||||
dom, err := parse(p, line)
|
||||
|
||||
// store only the last error
|
||||
if err != nil {
|
||||
p.lerr = err
|
||||
return
|
||||
}
|
||||
|
||||
push(p, dom)
|
||||
}
|
||||
|
||||
// parse parses the given text and returns a domain struct
|
||||
func parse(p *parser, line string) (dom domain, err error) {
|
||||
p.lines++
|
||||
|
||||
fields := strings.Fields(line)
|
||||
if len(fields) != 2 {
|
||||
err = fmt.Errorf("wrong input: %v (line #%d)", fields, p.lines)
|
||||
return
|
||||
}
|
||||
|
||||
dom.name = fields[0]
|
||||
|
||||
dom.visits, err = strconv.Atoi(fields[1])
|
||||
if dom.visits < 0 || err != nil {
|
||||
err = fmt.Errorf("wrong input: %q (line #%d)", fields[1], p.lines)
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// push pushes the given domain to the internal list of domains.
|
||||
// it also increases the total visits for all the domains.
|
||||
func push(p *parser, d domain) {
|
||||
// TODO:
|
||||
// if _, ok := p.sum[d.name]; !ok {
|
||||
// p.domains = append(p.domains, d)
|
||||
// }
|
||||
|
||||
// p.sum[d.name] += d.visits
|
||||
// p.total += d.visits
|
||||
name := d.name
|
||||
|
||||
// collect the unique domains
|
||||
if _, ok := p.sum[name]; !ok {
|
||||
p.domains = append(p.domains, name)
|
||||
}
|
||||
|
||||
p.total += d.visits
|
||||
d.visits += p.sum[name].visits
|
||||
p.sum[name] = d
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
learngoprogramming.com 10
|
||||
learngoprogramming.com 10
|
||||
golang.org 4
|
||||
golang.org 6
|
||||
blog.golang.org 20
|
||||
blog.golang.org 10
|
||||
@@ -0,0 +1,6 @@
|
||||
learngoprogramming.com 10
|
||||
learngoprogramming.com 10
|
||||
golang.org
|
||||
golang.org 6
|
||||
blog.golang.org 20
|
||||
blog.golang.org 10
|
||||
@@ -0,0 +1,6 @@
|
||||
learngoprogramming.com 10
|
||||
learngoprogramming.com 10
|
||||
golang.org -100
|
||||
golang.org 6
|
||||
blog.golang.org 20
|
||||
blog.golang.org 10
|
||||
@@ -0,0 +1,6 @@
|
||||
learngoprogramming.com 10
|
||||
learngoprogramming.com 10
|
||||
golang.org FOUR
|
||||
golang.org 6
|
||||
blog.golang.org 20
|
||||
blog.golang.org 10
|
||||
57
25-functions-and-pointers/08x-log-parser-pointers/main.go
Normal file
57
25-functions-and-pointers/08x-log-parser-pointers/main.go
Normal file
@@ -0,0 +1,57 @@
|
||||
// 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 (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"os"
|
||||
)
|
||||
|
||||
func main() {
|
||||
in := bufio.NewScanner(os.Stdin)
|
||||
|
||||
p := newParser()
|
||||
for in.Scan() {
|
||||
// dom, err := parse(p, in.Text())
|
||||
// if err != nil {
|
||||
// fmt.Println(err)
|
||||
// return
|
||||
// }
|
||||
|
||||
// p = push(p, dom)
|
||||
|
||||
add(p, in.Text())
|
||||
}
|
||||
|
||||
summarize(p)
|
||||
dumpErrs(in.Err(), err(p))
|
||||
}
|
||||
|
||||
// funcs not always need to be reused.
|
||||
// here, it tells about what it does: it summarizes the parsing result.
|
||||
func summarize(p *parser) {
|
||||
// multiple iterators can be created. each one remembers the last
|
||||
// read domain record.
|
||||
|
||||
next, cur := iterator(p)
|
||||
for next() {
|
||||
dom := cur()
|
||||
fmt.Printf("%-25s -> %d\n", dom.name, dom.visits)
|
||||
}
|
||||
fmt.Printf("\n%-25s -> %d\n", "TOTAL", p.total)
|
||||
}
|
||||
|
||||
// this variadic func simplifies the multiple error handling
|
||||
func dumpErrs(errs ...error) {
|
||||
for _, err := range errs {
|
||||
if err != nil {
|
||||
fmt.Printf("> Err: %s\n", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
110
25-functions-and-pointers/08x-log-parser-pointers/parser.go
Normal file
110
25-functions-and-pointers/08x-log-parser-pointers/parser.go
Normal file
@@ -0,0 +1,110 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// domain represents a domain log record
|
||||
type domain struct {
|
||||
name string
|
||||
visits int
|
||||
}
|
||||
|
||||
// parser parses a log file and provides an iterator to iterate upon the domains
|
||||
//
|
||||
// the parser struct is carefully crafted to be usable using its zero values except the map field
|
||||
type parser struct {
|
||||
sum map[string]domain // visits per unique domain
|
||||
domains []string // unique domain names
|
||||
total int // total visits to all domains
|
||||
lines int // number of parsed lines (for the error messages)
|
||||
lerr error // saves the last error occurred
|
||||
}
|
||||
|
||||
// newParser creates and returns a new parser.
|
||||
func newParser() *parser {
|
||||
return &parser{sum: make(map[string]domain)}
|
||||
}
|
||||
|
||||
// add parses the given line and saves the result to the internal list of
|
||||
// domains. it doesn't add the record when the parsing fails.
|
||||
func add(p *parser, line string) {
|
||||
// if there was a previous error do not add
|
||||
if p.lerr != nil {
|
||||
return
|
||||
}
|
||||
|
||||
dom, err := parse(p, line)
|
||||
|
||||
// store only the last error
|
||||
if err != nil {
|
||||
p.lerr = err
|
||||
return
|
||||
}
|
||||
|
||||
push(p, dom)
|
||||
}
|
||||
|
||||
// iterator returns two functions for iterating over domains.
|
||||
// next = returns true when there are more domains to iterate on.
|
||||
// cur = returns the current domain
|
||||
func iterator(p *parser) (next func() bool, cur func() domain) {
|
||||
// remember the last received line
|
||||
var last int
|
||||
|
||||
next = func() bool {
|
||||
defer func() { last++ }()
|
||||
return len(p.domains) > last
|
||||
}
|
||||
|
||||
cur = func() domain {
|
||||
// return a copy so the caller cannot change it
|
||||
name := p.domains[last-1]
|
||||
return p.sum[name]
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// error returns the last error occurred
|
||||
func err(p *parser) error {
|
||||
return p.lerr
|
||||
}
|
||||
|
||||
// parse parses the given text and returns a domain struct
|
||||
func parse(p *parser, line string) (dom domain, err error) {
|
||||
p.lines++ // increase the parsed line counter (only write is here)
|
||||
|
||||
fields := strings.Fields(line)
|
||||
if len(fields) != 2 {
|
||||
err = fmt.Errorf("wrong input: %v (line #%d)", fields, p.lines)
|
||||
return
|
||||
}
|
||||
|
||||
name, visits := fields[0], fields[1]
|
||||
|
||||
n, err := strconv.Atoi(visits)
|
||||
if n < 0 || err != nil {
|
||||
err = fmt.Errorf("wrong input: %q (line #%d)", visits, p.lines)
|
||||
return
|
||||
}
|
||||
|
||||
return domain{name: name, visits: n}, nil
|
||||
}
|
||||
|
||||
// push pushes the given domain to the internal list of domains.
|
||||
// it also increases the total visits for all the domains.
|
||||
func push(p *parser, d domain) {
|
||||
name := d.name
|
||||
|
||||
// collect the unique domains
|
||||
if _, ok := p.sum[name]; !ok {
|
||||
p.domains = append(p.domains, name)
|
||||
}
|
||||
|
||||
p.total += d.visits
|
||||
d.visits += p.sum[name].visits
|
||||
p.sum[name] = d
|
||||
}
|
||||
1
25-functions-and-pointers/README.md
Normal file
1
25-functions-and-pointers/README.md
Normal file
@@ -0,0 +1 @@
|
||||
This section is in progress. I'm working hard to update the course all the time. Hold on!
|
||||
6
25-functions-and-pointers/_WIP/log.txt
Normal file
6
25-functions-and-pointers/_WIP/log.txt
Normal file
@@ -0,0 +1,6 @@
|
||||
learngoprogramming.com 10
|
||||
learngoprogramming.com 10
|
||||
golang.org 4
|
||||
golang.org 6
|
||||
blog.golang.org 20
|
||||
blog.golang.org 10
|
||||
6
25-functions-and-pointers/_WIP/log_err_missing.txt
Normal file
6
25-functions-and-pointers/_WIP/log_err_missing.txt
Normal file
@@ -0,0 +1,6 @@
|
||||
learngoprogramming.com 10
|
||||
learngoprogramming.com 10
|
||||
golang.org
|
||||
golang.org 6
|
||||
blog.golang.org 20
|
||||
blog.golang.org 10
|
||||
6
25-functions-and-pointers/_WIP/log_err_negative.txt
Normal file
6
25-functions-and-pointers/_WIP/log_err_negative.txt
Normal file
@@ -0,0 +1,6 @@
|
||||
learngoprogramming.com 10
|
||||
learngoprogramming.com 10
|
||||
golang.org -100
|
||||
golang.org 6
|
||||
blog.golang.org 20
|
||||
blog.golang.org 10
|
||||
6
25-functions-and-pointers/_WIP/log_err_str.txt
Normal file
6
25-functions-and-pointers/_WIP/log_err_str.txt
Normal file
@@ -0,0 +1,6 @@
|
||||
learngoprogramming.com 10
|
||||
learngoprogramming.com 10
|
||||
golang.org FOUR
|
||||
golang.org 6
|
||||
blog.golang.org 20
|
||||
blog.golang.org 10
|
||||
Reference in New Issue
Block a user