update: log parser dir to input scanning
This commit is contained in:
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)
|
||||
}
|
@ -18,7 +18,6 @@ package main
|
||||
// 2. Scan the input using a new Scanner.
|
||||
//
|
||||
// 3. Configure the scanner to scan for the words.
|
||||
// See the explanation inside the code below.
|
||||
//
|
||||
// 4. Count the unique words using a map.
|
||||
//
|
||||
@ -27,13 +26,4 @@ package main
|
||||
// ---------------------------------------------------------
|
||||
|
||||
func main() {
|
||||
// I'll talk about this in the function values section later on.
|
||||
//
|
||||
// Simply put:
|
||||
// Scanner can scan the lines, words, anything.
|
||||
// Use the following code after creating the scanner
|
||||
// to scan for the words instead.
|
||||
// Below, I assumed that you put your scanner into the "in" variable.
|
||||
|
||||
// in.Split(bufio.ScanWords)
|
||||
}
|
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
|
Reference in New Issue
Block a user