create first counter
This commit is contained in:
parent
2de583632e
commit
d8ea359d48
4 changed files with 146 additions and 0 deletions
3
Lab04/code/part01/go.mod
Normal file
3
Lab04/code/part01/go.mod
Normal file
|
@ -0,0 +1,3 @@
|
|||
module frequency
|
||||
|
||||
go 1.18
|
27
Lab04/code/part01/main.go
Normal file
27
Lab04/code/part01/main.go
Normal file
|
@ -0,0 +1,27 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func wordCount(str string) map[string]int {
|
||||
wordList := strings.Fields(str)
|
||||
counts := make(map[string]int)
|
||||
for _, word := range wordList {
|
||||
_, ok := counts[word]
|
||||
if ok {
|
||||
counts[word] += 1
|
||||
} else {
|
||||
counts[word] = 1
|
||||
}
|
||||
}
|
||||
return counts
|
||||
}
|
||||
|
||||
func main() {
|
||||
strLine := "Australia Canada Germany Australia Japan Canada"
|
||||
for index,element := range wordCount(strLine){
|
||||
fmt.Println(index,"=>",element)
|
||||
}
|
||||
}
|
17
Lab04/code/part01/makefile
Normal file
17
Lab04/code/part01/makefile
Normal file
|
@ -0,0 +1,17 @@
|
|||
# Go parameters
|
||||
GoCMD=go
|
||||
GoBUILD=$(GoCMD) build
|
||||
GoCLEAN=$(GoCMD) clean
|
||||
GoTEST=$(GoCMD) test
|
||||
GoMOD=$(GoCMD) mod
|
||||
BINARY_NAME=frequency
|
||||
|
||||
# To install GoLang please follow the installation instructions on https://go.dev/
|
||||
all: build
|
||||
build:
|
||||
$(GoBUILD) -o $(BINARY_NAME) -v
|
||||
clean:
|
||||
$(GOCLEAN)
|
||||
rm -f $(BINARY_NAME)
|
||||
deps:
|
||||
$(GoMOD) tidy
|
Reference in a new issue