diff --git a/Homework_1/go.mod b/Homework_1/go.mod new file mode 100644 index 0000000..8fb439d --- /dev/null +++ b/Homework_1/go.mod @@ -0,0 +1,5 @@ +module letsgo/homework1 + +go 1.27.0 + +require golang.org/x/net v0.59.0 // indirect diff --git a/Homework_1/main.go b/Homework_1/main.go new file mode 100644 index 0000000..775f642 --- /dev/null +++ b/Homework_1/main.go @@ -0,0 +1,59 @@ +package main + +import ( + "bytes" + "fmt" + "os" + "strings" + + "golang.org/x/net/html" +) + +var raw = ` + + + +
+ node one image +

First node first child

+
+
+ node two image +

Second node second child

+
+
+ Third node second child +
+ + + ` + +func main() { + doc, err := html.Parse(bytes.NewReader([]byte(raw))) + if err != nil { + fmt.Fprintf(os.Stderr, "Parsing failed: %s\n", err) + os.Exit(-1) + } + + words, images := counterofwordsandimages(doc) + fmt.Printf("There are %d words and %d images\n", words, images) +} + +func counterofwordsandimages(doc *html.Node) (int, int) { + var words, images int + + visit(doc, &words, &images) + + return words, images +} + +func visit(n *html.Node, words, images *int) { + if n.Type == html.TextNode { + *words += len(strings.Fields(n.Data)) + } else if n.Type == html.ElementNode && n.Data == "img" { + *images++ + } + for c := n.FirstChild; c != nil; c = c.NextSibling { + visit(c, words, images) + } +} diff --git a/go.work b/go.work index 5cc5fb3..0fc29cb 100644 --- a/go.work +++ b/go.work @@ -3,6 +3,7 @@ go 1.27.0 use ( ./Composite_Types ./For_If_Switch + ./Homework_1 ./courses ./formatIO ./functions diff --git a/go.work.sum b/go.work.sum new file mode 100644 index 0000000..c12a7c5 --- /dev/null +++ b/go.work.sum @@ -0,0 +1,6 @@ +golang.org/x/crypto v0.57.0/go.mod h1:Fdz0i5U6CoizGwLda9DttjSk6qlZo25zYNtR+ycvuZA= +golang.org/x/net v0.59.0 h1:5zfYln+w5XCxwrnMMJPufRgNoXEaGxl0wo5GqPXyues= +golang.org/x/net v0.59.0/go.mod h1:2DA/G1UfVbCpQPeWTmMPGY7Cs2PkBkwu743bVX5PIVg= +golang.org/x/sys v0.48.0/go.mod h1:hNLxWAXmnKAxqDtdwIYC4bM9oQPEecfsnNMuSxOs3og= +golang.org/x/term v0.46.0/go.mod h1:+K02xbkittuwc0Am4abfA3Fc+XRGXkvBXNO88NCXPoc= +golang.org/x/text v0.42.0/go.mod h1:ojzP1Z+2QtioaF8DTtO8K5q7JWVVYwZKenzujK0Zd0E= diff --git a/slices/main.go b/slices/main.go index 1ca3f1f..f92e79a 100644 --- a/slices/main.go +++ b/slices/main.go @@ -17,11 +17,6 @@ func main() { s = append(s, 1, 1) fmt.Printf("%d, %d, %T, %5t, %#[3]v\n", len(s), cap(s), s, s == nil) - fmt.Println("*****************************") - fmt.Println("*****************************") - fmt.Println("*****************************") - fmt.Println("*****************************") - fmt.Println("*****************************") a := [...]int{1, 2, 3}