HomeWork_1

This commit is contained in:
2026-09-10 20:26:26 -04:00
parent dd7e213077
commit b0eff2bfd5
5 changed files with 71 additions and 5 deletions
+5
View File
@@ -0,0 +1,5 @@
module letsgo/homework1
go 1.27.0
require golang.org/x/net v0.59.0 // indirect
+59
View File
@@ -0,0 +1,59 @@
package main
import (
"bytes"
"fmt"
"os"
"strings"
"golang.org/x/net/html"
)
var raw = `
<!DOCTYPE html>
<html lang="en">
<body>
<div class="node1">
<img src="/images/node1.png" alt="node one image">
<p>First node first child</p>
</div>
<div class="node2">
<img src="/images/node2.png" alt="node two image">
<h1>Second node second child</h1>
</div>
<div class="node3">
<a href="https://example.com">Third node second child</a>
</div>
</body>
</html>
`
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)
}
}
+1
View File
@@ -3,6 +3,7 @@ go 1.27.0
use ( use (
./Composite_Types ./Composite_Types
./For_If_Switch ./For_If_Switch
./Homework_1
./courses ./courses
./formatIO ./formatIO
./functions ./functions
+6
View File
@@ -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=
-5
View File
@@ -17,11 +17,6 @@ func main() {
s = append(s, 1, 1) s = append(s, 1, 1)
fmt.Printf("%d, %d, %T, %5t, %#[3]v\n", len(s), cap(s), s, s == nil) 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} a := [...]int{1, 2, 3}