push all exercices
This commit is contained in:
BIN
ch6/1/ch6_1.exe
Normal file
BIN
ch6/1/ch6_1.exe
Normal file
Binary file not shown.
22
ch6/1/ch6_1.go
Normal file
22
ch6/1/ch6_1.go
Normal file
@@ -0,0 +1,22 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
type Person struct {
|
||||
FirstName string
|
||||
LastName string
|
||||
Age int
|
||||
}
|
||||
|
||||
func MakePerson(firstName, lastName string, age int) Person {
|
||||
return Person{firstName, lastName, age}
|
||||
}
|
||||
|
||||
func MakePersonPointer(firstName, lastName string, age int) *Person {
|
||||
return &Person{firstName, lastName, age}
|
||||
}
|
||||
|
||||
func main() {
|
||||
fmt.Println(MakePerson("Ivan", "Ivanov", 21))
|
||||
fmt.Println(*MakePersonPointer("Peter", "Petrov", 22))
|
||||
}
|
||||
3
ch6/1/go.mod
Normal file
3
ch6/1/go.mod
Normal file
@@ -0,0 +1,3 @@
|
||||
module ch6_1
|
||||
|
||||
go 1.25.0
|
||||
BIN
ch6/2/ch6_2.exe
Normal file
BIN
ch6/2/ch6_2.exe
Normal file
Binary file not shown.
30
ch6/2/ch6_2.go
Normal file
30
ch6/2/ch6_2.go
Normal file
@@ -0,0 +1,30 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
func UpdateSlice(strSlice []string, cmpStr string) {
|
||||
sliceSize := len(strSlice)
|
||||
for i := 0; i < sliceSize; i++ {
|
||||
if cmpStr == strSlice[i] {
|
||||
strSlice = strSlice[:i]
|
||||
break
|
||||
}
|
||||
}
|
||||
fmt.Println(strSlice)
|
||||
}
|
||||
|
||||
func GrowSlice(strSlice []string, appStr string) {
|
||||
strSlice = append(strSlice, appStr)
|
||||
fmt.Println(strSlice)
|
||||
}
|
||||
|
||||
func main() {
|
||||
strSlice := []string{"aaa", "bbb", "ccc", "ddd"}
|
||||
fmt.Println("Before function UpdateSlice: ", strSlice)
|
||||
UpdateSlice(strSlice, "ccc")
|
||||
fmt.Println("After function UpdateSlice: ", strSlice)
|
||||
strSlice = []string{"aaa", "bbb", "ccc", "ddd"}
|
||||
fmt.Println("Before function GrowSlice: ", strSlice)
|
||||
GrowSlice(strSlice, "eee")
|
||||
fmt.Println("After function GrowSlice: ", strSlice)
|
||||
}
|
||||
3
ch6/2/go.mod
Normal file
3
ch6/2/go.mod
Normal file
@@ -0,0 +1,3 @@
|
||||
module ch6_2
|
||||
|
||||
go 1.25.0
|
||||
BIN
ch6/3/ch6_3.exe
Normal file
BIN
ch6/3/ch6_3.exe
Normal file
Binary file not shown.
20
ch6/3/ch6_3.go
Normal file
20
ch6/3/ch6_3.go
Normal file
@@ -0,0 +1,20 @@
|
||||
package main
|
||||
|
||||
import "fmt"
|
||||
|
||||
type Person struct {
|
||||
name string
|
||||
age int
|
||||
}
|
||||
|
||||
func MakePerson(name string, age int) Person {
|
||||
return Person{name, age}
|
||||
}
|
||||
|
||||
func main() {
|
||||
personSlice := make([]Person, 0, 512)
|
||||
for i := 0; i < 10_000_000; i++ {
|
||||
personSlice = append(personSlice, MakePerson("aaa", i))
|
||||
}
|
||||
fmt.Println(personSlice[:100])
|
||||
}
|
||||
3
ch6/3/go.mod
Normal file
3
ch6/3/go.mod
Normal file
@@ -0,0 +1,3 @@
|
||||
module ch6_3
|
||||
|
||||
go 1.25.0
|
||||
Reference in New Issue
Block a user