update: log parser dir to input scanning
This commit is contained in:
41
23-input-scanning/01-scanning/main.go
Normal file
41
23-input-scanning/01-scanning/main.go
Normal file
@ -0,0 +1,41 @@
|
||||
// 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() {
|
||||
// Create a new scanner that scans from the standard-input
|
||||
in := bufio.NewScanner(os.Stdin)
|
||||
|
||||
// Simulate an error
|
||||
// os.Stdin.Close()
|
||||
|
||||
// Stores the total number of lines in the input
|
||||
var lines int
|
||||
|
||||
// Scan the input line by line
|
||||
for in.Scan() {
|
||||
lines++
|
||||
|
||||
// Get the current line from the scanner's buffer
|
||||
// fmt.Println("Scanned Text :", in.Text())
|
||||
// fmt.Println("Scanned Bytes:", in.Bytes())
|
||||
in.Text()
|
||||
}
|
||||
fmt.Printf("There %d lines.\n", lines)
|
||||
|
||||
err := in.Err()
|
||||
if err != nil {
|
||||
fmt.Println("ERROR:", err)
|
||||
}
|
||||
}
|
4
23-input-scanning/01-scanning/proverbs.txt
Normal file
4
23-input-scanning/01-scanning/proverbs.txt
Normal file
@ -0,0 +1,4 @@
|
||||
Go Proverbs
|
||||
Make the zero value useful
|
||||
Clear is better than clever
|
||||
Errors are values
|
44
23-input-scanning/02-map-as-sets/main.go
Normal file
44
23-input-scanning/02-map-as-sets/main.go
Normal file
@ -0,0 +1,44 @@
|
||||
package main
|
||||
|
||||
// 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/
|
||||
//
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"os"
|
||||
)
|
||||
|
||||
func main() {
|
||||
args := os.Args[1:]
|
||||
if len(args) != 1 {
|
||||
fmt.Println("Please type a search word.")
|
||||
return
|
||||
}
|
||||
query := args[0]
|
||||
|
||||
in := bufio.NewScanner(os.Stdin)
|
||||
in.Split(bufio.ScanWords)
|
||||
|
||||
// index the words
|
||||
words := make(map[string]bool)
|
||||
for in.Scan() {
|
||||
words[in.Text()] = true
|
||||
}
|
||||
|
||||
// unnecessary
|
||||
// if _, ok := words[query]; ok {
|
||||
// ...
|
||||
// }
|
||||
|
||||
// answer the user query
|
||||
if words[query] {
|
||||
fmt.Printf("The input contains %q word.\n", query)
|
||||
return
|
||||
}
|
||||
fmt.Printf("Sorry. The input does contain %q word.\n", query)
|
||||
}
|
12
23-input-scanning/02-map-as-sets/shakespeare.txt
Normal file
12
23-input-scanning/02-map-as-sets/shakespeare.txt
Normal file
@ -0,0 +1,12 @@
|
||||
come night come romeo come thou day in night
|
||||
for thou wilt lie upon the wings of night
|
||||
whiter than new snow on a raven's back
|
||||
come gentle night come loving black-browed night
|
||||
give me my romeo and when he shall die
|
||||
take him and cut him out in little stars
|
||||
and he will make the face of heaven so fine
|
||||
that all the world will be in love with night
|
||||
and pay no worship to the garish sun
|
||||
oh i have bought the mansion of love
|
||||
but not possessed it and though i am sold
|
||||
not yet enjoyed
|
6
23-input-scanning/03-project-log-parser/log.txt
Normal file
6
23-input-scanning/03-project-log-parser/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
|
6
23-input-scanning/03-project-log-parser/log_err_str.txt
Normal file
6
23-input-scanning/03-project-log-parser/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
|
78
23-input-scanning/03-project-log-parser/main.go
Normal file
78
23-input-scanning/03-project-log-parser/main.go
Normal file
@ -0,0 +1,78 @@
|
||||
// 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"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func main() {
|
||||
var (
|
||||
sum map[string]int // total visits per domain
|
||||
domains []string // unique domain names
|
||||
total int // total visits to all domains
|
||||
lines int // number of parsed lines (for the error messages)
|
||||
)
|
||||
|
||||
sum = make(map[string]int)
|
||||
|
||||
// Scan the standard-in line by line
|
||||
in := bufio.NewScanner(os.Stdin)
|
||||
for in.Scan() {
|
||||
lines++
|
||||
|
||||
// Parse the fields
|
||||
fields := strings.Fields(in.Text())
|
||||
if len(fields) != 2 {
|
||||
fmt.Printf("wrong input: %v (line #%d)\n", fields, lines)
|
||||
return
|
||||
}
|
||||
|
||||
domain := fields[0]
|
||||
|
||||
// Sum the total visits per domain
|
||||
visits, err := strconv.Atoi(fields[1])
|
||||
if visits < 0 || err != nil {
|
||||
fmt.Printf("wrong input: %q (line #%d)\n", fields[1], lines)
|
||||
return
|
||||
}
|
||||
|
||||
// Collect the unique domains
|
||||
if _, ok := sum[domain]; !ok {
|
||||
domains = append(domains, domain)
|
||||
}
|
||||
|
||||
// Keep track of total and per domain visits
|
||||
total += visits
|
||||
sum[domain] += visits
|
||||
}
|
||||
|
||||
// Print the visits per domain
|
||||
sort.Strings(domains)
|
||||
|
||||
fmt.Printf("%-30s %10s\n", "DOMAIN", "VISITS")
|
||||
fmt.Println(strings.Repeat("-", 45))
|
||||
|
||||
for _, domain := range domains {
|
||||
visits := sum[domain]
|
||||
fmt.Printf("%-30s %10d\n", domain, visits)
|
||||
}
|
||||
|
||||
// Print the total visits for all domains
|
||||
fmt.Printf("\n%-30s %10d\n", "TOTAL", total)
|
||||
|
||||
// Let's handle the error
|
||||
if err := in.Err(); err != nil {
|
||||
fmt.Println("> Err:", err)
|
||||
}
|
||||
}
|
20
23-input-scanning/exercises/01-uppercaser/main.go
Normal file
20
23-input-scanning/exercises/01-uppercaser/main.go
Normal file
@ -0,0 +1,20 @@
|
||||
package main
|
||||
|
||||
// ---------------------------------------------------------
|
||||
// EXERCISE: Uppercaser
|
||||
//
|
||||
// Use a scanner to convert the lines to uppercase, and
|
||||
// print them.
|
||||
//
|
||||
// 1. Feed the shakespeare.txt to your program.
|
||||
//
|
||||
// 2. Scan the input using a new Scanner.
|
||||
//
|
||||
// 3. Print each line.
|
||||
//
|
||||
// EXPECTED OUTPUT
|
||||
// Please run the solution to see the expected output.
|
||||
// ---------------------------------------------------------
|
||||
|
||||
func main() {
|
||||
}
|
12
23-input-scanning/exercises/01-uppercaser/shakespeare.txt
Normal file
12
23-input-scanning/exercises/01-uppercaser/shakespeare.txt
Normal file
@ -0,0 +1,12 @@
|
||||
come night come romeo come thou day in night
|
||||
for thou wilt lie upon the wings of night
|
||||
whiter than new snow on a raven's back
|
||||
come gentle night come loving black-browed night
|
||||
give me my romeo and when he shall die
|
||||
take him and cut him out in little stars
|
||||
and he will make the face of heaven so fine
|
||||
that all the world will be in love with night
|
||||
and pay no worship to the garish sun
|
||||
oh i have bought the mansion of love
|
||||
but not possessed it and though i am sold
|
||||
not yet enjoyed
|
23
23-input-scanning/exercises/01-uppercaser/solution/main.go
Normal file
23
23-input-scanning/exercises/01-uppercaser/solution/main.go
Normal file
@ -0,0 +1,23 @@
|
||||
// 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"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func main() {
|
||||
in := bufio.NewScanner(os.Stdin)
|
||||
|
||||
for in.Scan() {
|
||||
fmt.Println(strings.ToUpper(in.Text()))
|
||||
}
|
||||
}
|
@ -0,0 +1,12 @@
|
||||
come night come romeo come thou day in night
|
||||
for thou wilt lie upon the wings of night
|
||||
whiter than new snow on a raven's back
|
||||
come gentle night come loving black-browed night
|
||||
give me my romeo and when he shall die
|
||||
take him and cut him out in little stars
|
||||
and he will make the face of heaven so fine
|
||||
that all the world will be in love with night
|
||||
and pay no worship to the garish sun
|
||||
oh i have bought the mansion of love
|
||||
but not possessed it and though i am sold
|
||||
not yet enjoyed
|
29
23-input-scanning/exercises/02-unique-words/main.go
Normal file
29
23-input-scanning/exercises/02-unique-words/main.go
Normal file
@ -0,0 +1,29 @@
|
||||
// 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
|
||||
|
||||
// ---------------------------------------------------------
|
||||
// EXERCISE: Unique Words
|
||||
//
|
||||
// Create a program that counts the unique words from an
|
||||
// input stream.
|
||||
//
|
||||
// 1. Feed the shakespeare.txt to your program.
|
||||
//
|
||||
// 2. Scan the input using a new Scanner.
|
||||
//
|
||||
// 3. Configure the scanner to scan for the words.
|
||||
//
|
||||
// 4. Count the unique words using a map.
|
||||
//
|
||||
// EXPECTED OUTPUT
|
||||
// There are 99 words, 70 of them are unique.
|
||||
// ---------------------------------------------------------
|
||||
|
||||
func main() {
|
||||
}
|
12
23-input-scanning/exercises/02-unique-words/shakespeare.txt
Normal file
12
23-input-scanning/exercises/02-unique-words/shakespeare.txt
Normal file
@ -0,0 +1,12 @@
|
||||
come night come romeo come thou day in night
|
||||
for thou wilt lie upon the wings of night
|
||||
whiter than new snow on a raven's back
|
||||
come gentle night come loving black-browed night
|
||||
give me my romeo and when he shall die
|
||||
take him and cut him out in little stars
|
||||
and he will make the face of heaven so fine
|
||||
that all the world will be in love with night
|
||||
and pay no worship to the garish sun
|
||||
oh i have bought the mansion of love
|
||||
but not possessed it and though i am sold
|
||||
not yet enjoyed
|
27
23-input-scanning/exercises/02-unique-words/solution/main.go
Normal file
27
23-input-scanning/exercises/02-unique-words/solution/main.go
Normal file
@ -0,0 +1,27 @@
|
||||
package main
|
||||
|
||||
// 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/
|
||||
//
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"os"
|
||||
)
|
||||
|
||||
func main() {
|
||||
in := bufio.NewScanner(os.Stdin)
|
||||
in.Split(bufio.ScanWords)
|
||||
|
||||
total, words := 0, make(map[string]int)
|
||||
for in.Scan() {
|
||||
total++
|
||||
words[in.Text()]++
|
||||
}
|
||||
fmt.Printf("There are %d words, %d of them are unique.\n",
|
||||
total, len(words))
|
||||
}
|
@ -0,0 +1,12 @@
|
||||
come night come romeo come thou day in night
|
||||
for thou wilt lie upon the wings of night
|
||||
whiter than new snow on a raven's back
|
||||
come gentle night come loving black-browed night
|
||||
give me my romeo and when he shall die
|
||||
take him and cut him out in little stars
|
||||
and he will make the face of heaven so fine
|
||||
that all the world will be in love with night
|
||||
and pay no worship to the garish sun
|
||||
oh i have bought the mansion of love
|
||||
but not possessed it and though i am sold
|
||||
not yet enjoyed
|
25
23-input-scanning/exercises/03-grep/main.go
Normal file
25
23-input-scanning/exercises/03-grep/main.go
Normal file
@ -0,0 +1,25 @@
|
||||
package main
|
||||
|
||||
// ---------------------------------------------------------
|
||||
// EXERCISE: Grep Clone
|
||||
//
|
||||
// Create a grep clone. grep is a command-line utility for
|
||||
// searching plain-text data for lines that match a specific
|
||||
// pattern.
|
||||
//
|
||||
// 1. Feed the shakespeare.txt to your program.
|
||||
//
|
||||
// 2. Accept a command-line argument for the pattern
|
||||
//
|
||||
// 3. Only print the lines that contains that pattern
|
||||
//
|
||||
// 4. If no pattern is provided, print all the lines
|
||||
//
|
||||
// EXPECTED OUTPUT
|
||||
// go run main.go come < shakespeare.txt
|
||||
// come night come romeo come thou day in night
|
||||
// come gentle night come loving black-browed night
|
||||
// ---------------------------------------------------------
|
||||
|
||||
func main() {
|
||||
}
|
12
23-input-scanning/exercises/03-grep/shakespeare.txt
Normal file
12
23-input-scanning/exercises/03-grep/shakespeare.txt
Normal file
@ -0,0 +1,12 @@
|
||||
come night come romeo come thou day in night
|
||||
for thou wilt lie upon the wings of night
|
||||
whiter than new snow on a raven's back
|
||||
come gentle night come loving black-browed night
|
||||
give me my romeo and when he shall die
|
||||
take him and cut him out in little stars
|
||||
and he will make the face of heaven so fine
|
||||
that all the world will be in love with night
|
||||
and pay no worship to the garish sun
|
||||
oh i have bought the mansion of love
|
||||
but not possessed it and though i am sold
|
||||
not yet enjoyed
|
31
23-input-scanning/exercises/03-grep/solution/main.go
Normal file
31
23-input-scanning/exercises/03-grep/solution/main.go
Normal file
@ -0,0 +1,31 @@
|
||||
// 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"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func main() {
|
||||
in := bufio.NewScanner(os.Stdin)
|
||||
|
||||
var pattern string
|
||||
if args := os.Args[1:]; len(args) == 1 {
|
||||
pattern = args[0]
|
||||
}
|
||||
|
||||
for in.Scan() {
|
||||
s := in.Text()
|
||||
if strings.Contains(s, pattern) {
|
||||
fmt.Println(s)
|
||||
}
|
||||
}
|
||||
}
|
12
23-input-scanning/exercises/03-grep/solution/shakespeare.txt
Normal file
12
23-input-scanning/exercises/03-grep/solution/shakespeare.txt
Normal file
@ -0,0 +1,12 @@
|
||||
come night come romeo come thou day in night
|
||||
for thou wilt lie upon the wings of night
|
||||
whiter than new snow on a raven's back
|
||||
come gentle night come loving black-browed night
|
||||
give me my romeo and when he shall die
|
||||
take him and cut him out in little stars
|
||||
and he will make the face of heaven so fine
|
||||
that all the world will be in love with night
|
||||
and pay no worship to the garish sun
|
||||
oh i have bought the mansion of love
|
||||
but not possessed it and though i am sold
|
||||
not yet enjoyed
|
15
23-input-scanning/exercises/README.md
Normal file
15
23-input-scanning/exercises/README.md
Normal file
@ -0,0 +1,15 @@
|
||||
# Exercises
|
||||
|
||||
Let's exercise with the scanner and maps.
|
||||
|
||||
1. **[Uppercaser](https://github.com/inancgumus/learngo/tree/master/23-project-log-parser/exercises/01-uppercaser)**
|
||||
|
||||
Use a scanner to convert the lines to uppercase, and print them.
|
||||
|
||||
2. **[Unique Words](https://github.com/inancgumus/learngo/tree/master/23-project-log-parser/exercises/02-unique-words)**
|
||||
|
||||
Create a program that counts the unique words from an input stream.
|
||||
|
||||
3. **[Grep Clone](https://github.com/inancgumus/learngo/tree/master/23-project-log-parser/exercises/03-grep)**
|
||||
|
||||
Create a grep clone. grep is a command-line utility for searching plain-text data for lines that match a specific pattern.
|
Reference in New Issue
Block a user