structs with packages

This commit is contained in:
Aliberk Sandıkçı 2023-07-24 10:37:47 +03:00
parent 016b14ac21
commit 7d794d53e6
Signed by: asandikci
GPG Key ID: 25C67A03B5666BC1
2 changed files with 42 additions and 0 deletions

View File

@ -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)
}

View File

@ -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)")
}