dtelenkov hai 4 días
pai
achega
073038a3c8
Modificáronse 2 ficheiros con 101 adicións e 0 borrados
  1. 17 0
      go/stepik/1/pointer.go
  2. 84 0
      go/stepik/1/struct.go

+ 17 - 0
go/stepik/1/pointer.go

@@ -0,0 +1,17 @@
+package main
+
+import "fmt"
+
+func main() {
+	// ptr := new(int)
+
+	a := 3
+	b := 4
+	test(&a, &b)
+}
+
+func test(x1 *int, x2 *int) {
+	// fmt.Println(*x1 * *x2)
+	*x1, *x2 = *x2, *x1
+	fmt.Println(*x1, *x2)
+}

+ 84 - 0
go/stepik/1/struct.go

@@ -0,0 +1,84 @@
+package main
+
+import (
+	"fmt"
+	"math"
+)
+
+type Circle struct {
+	x, y, r float64
+}
+
+type Rectangle struct {
+	x1, y1, x2, y2 float64
+}
+
+func (c Circle) area() float64 {
+	return math.Pi * c.r * c.r
+}
+
+func (r Rectangle) area() float64 {
+	l := r.x2 - r.x1
+	w := r.y2 - r.y1
+	return l * w
+}
+
+type Person struct {
+	Name string
+}
+
+func (p *Person) Talk() {
+	fmt.Println("Hi, my name is", p.Name)
+}
+
+type Android struct {
+	Person
+	Model string
+}
+
+type Character struct {
+	On    bool
+	Ammo  int
+	Power int
+}
+
+func (c *Character) Shoot() bool {
+	if c.On == false {
+		return false
+	} else if c.Ammo > 0 {
+		c.Ammo--
+		return true
+	} else {
+		return false
+	}
+}
+
+func (c *Character) RideBike() bool {
+	if c.On == false {
+		return false
+	} else if c.Power > 0 {
+		c.Power--
+		return true
+	} else {
+		return false
+	}
+}
+
+func main() {
+	/*
+		rect := Rectangle{x1: 0, y1: 0, x2: 10, y2: 10}
+		circ := Circle{x: 0, y: 0, r: 5}
+		fmt.Println("Rectangle area:", rect.area())
+		fmt.Println("Circle area:", circ.area())
+	*/
+	/*
+		a := new(Android)
+		a.Name = "Fedor"
+		a.Person.Talk()
+		a.Talk()
+	*/
+	ch := Character{true, 5, 10}
+
+	fmt.Println(ch.Shoot(), ch.Ammo, ch.Power)
+	fmt.Println(ch.Shoot(), ch.Ammo, ch.Power)
+}