From 7d794d53e691998fa8942b43f45f37d97bc76a93 Mon Sep 17 00:00:00 2001 From: asandikci Date: Mon, 24 Jul 2023 10:37:47 +0300 Subject: [PATCH] structs with packages --- simple-structure/package-str/example/main.go | 15 +++++++++++ simple-structure/package-str/great/great.go | 27 ++++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/simple-structure/package-str/example/main.go b/simple-structure/package-str/example/main.go index 0907b98..6f1ba55 100644 --- a/simple-structure/package-str/example/main.go +++ b/simple-structure/package-str/example/main.go @@ -9,4 +9,19 @@ func main() { great.Hello() fmt.Println(great.Name) // fmt.Println(great.name2) // ERROR lowercase variables cannot be used outside of package itself + + car1 := great.Car{Name: "bmw", Color: "red"} + // car1_1 := great.Car{"bmw", "red"} // TODO WHY THIS GIVES WARNING + fmt.Println(car1) // same with fmt.Println(car1.String()), bcs fmt uses String() by default + + // car2 := great.car2{Name: "ford", Color: "green"} // ERROR lowercase structs cannot be used outside of package itself + + fmt.Printf("\n\n") + + // car3 := great.Car3{Name: "ford", color: "green"} // unknown field color, also lowercase struct variables cannot be used outside of package itself + + car3_1 := great.Car3{} + car3_1.Name = "ford" + // car3_1.color = "green" // undefined + fmt.Println(car3_1) } diff --git a/simple-structure/package-str/great/great.go b/simple-structure/package-str/great/great.go index e91fb58..a910195 100644 --- a/simple-structure/package-str/great/great.go +++ b/simple-structure/package-str/great/great.go @@ -5,6 +5,33 @@ import "fmt" var Name = "deniz" var name2 = "deniz2" +type Car struct { + Name string + Color string +} + +func (c Car) String() string { + return fmt.Sprintf("car color:%q | name:%q", c.Color, c.Name) +} + +type car2 struct { + Name string + Color string +} + +func (c car2) String() string { + return fmt.Sprintf("car color:%q | name:%q", c.Color, c.Name) +} + +type Car3 struct { + Name string + color string +} + +func (c Car3) String() string { + return fmt.Sprintf("car color:%q | name:%q", c.color, c.Name) +} + func Hello() { fmt.Println("Hello World (go-organization/simple-structure/package-str/great/great.go)") }