Merge branch 'main' of https://gitea.zacksolutions.dev/Zakaria/letsGO
This commit is contained in:
@@ -0,0 +1,5 @@
|
||||
module letsgo/homework1
|
||||
|
||||
go 1.27.0
|
||||
|
||||
require golang.org/x/net v0.59.0 // indirect
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
module main
|
||||
|
||||
go 1.27.0
|
||||
@@ -0,0 +1,21 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func fib() func() int {
|
||||
a, b := 0, 1
|
||||
return func() int {
|
||||
a, b = b, a+b
|
||||
return b
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
f := fib()
|
||||
fmt.Println("This Fibonacci sequense < 10000")
|
||||
for x := f(); x < 10000; x = f() {
|
||||
fmt.Println(x)
|
||||
}
|
||||
}
|
||||
@@ -1,19 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
const name = "Sara"
|
||||
|
||||
love := "Kahlousha"
|
||||
number := 43900900
|
||||
|
||||
func main() {
|
||||
|
||||
const world = "Mykids"
|
||||
fmt.Println("Hello,", world)
|
||||
|
||||
fmt.Println("I love ", name)
|
||||
|
||||
}
|
||||
@@ -3,6 +3,7 @@ go 1.27.1
|
||||
use (
|
||||
./Composite_Types
|
||||
./For_If_Switch
|
||||
./Homework_1
|
||||
./courses
|
||||
./formatIO
|
||||
./functions
|
||||
|
||||
@@ -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=
|
||||
@@ -6,7 +6,6 @@ import (
|
||||
)
|
||||
|
||||
func main() {
|
||||
|
||||
var sum float64
|
||||
var n int
|
||||
|
||||
@@ -27,5 +26,4 @@ func main() {
|
||||
}
|
||||
average := sum / float64(n)
|
||||
fmt.Println("The average of the total numbers is:", average)
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
module main
|
||||
|
||||
go 1.27.0
|
||||
@@ -0,0 +1,30 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func main() {
|
||||
var s []int
|
||||
t := []int{}
|
||||
u := make([]string, 5)
|
||||
v := make([]string, 0, 5)
|
||||
|
||||
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(t), cap(t), t, t == nil)
|
||||
fmt.Printf("%d, %d, %T, %5t, %#[3]v\n", len(u), cap(u), u, u == nil)
|
||||
fmt.Printf("%d, %d, %T, %5t, %#[3]v\n", len(v), cap(v), v, v == nil)
|
||||
|
||||
s = append(s, 1, 1)
|
||||
fmt.Printf("%d, %d, %T, %5t, %#[3]v\n", len(s), cap(s), s, s == nil)
|
||||
|
||||
a := [...]int{1, 2, 3}
|
||||
|
||||
b := a[:1]
|
||||
|
||||
c := b[:2]
|
||||
|
||||
fmt.Printf("a[%p] = %v\n", &a, a)
|
||||
fmt.Printf("b[%p] = %[1]v\n", b)
|
||||
fmt.Printf("c[%p] = %[1]v\n", c)
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
module main
|
||||
|
||||
go 1.27.1
|
||||
@@ -0,0 +1,19 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
type Response struct {
|
||||
Page int `json:"page"`
|
||||
Words []string `json:"words, omitempty"`
|
||||
}
|
||||
|
||||
func main() {
|
||||
r := &Response{Page: 1, Words: []string{"Leena", "Sami", "Sara", "Zaki"}}
|
||||
|
||||
j, _ := json.MarshalIndent(r, "", " ")
|
||||
|
||||
fmt.Println(string(j))
|
||||
}
|
||||
Reference in New Issue
Block a user