Skip to content

Instantly share code, notes, and snippets.

View kadukf-outreach's full-sized avatar

kadukf-outreach

View GitHub Profile
//./shape.go:83:6: cannot use NewSquare(5) (type *Square) as type Shape in assignment:
// *Square does not implement Shape (missing Area method)
type Shape interface {
Area() int
}
type Square struct {
a int
}
func NewSquare(a int) *Square {
return &Square{a: a}
type Shape interface {
Area() int
}
type Square struct {
a int
}
func NewSquare(a int) *Square {
return &Square{a: a}
type Square struct {
Shape
Rectangle
a int
}
func main() {
s := NewSquare(5)
s.Shape = NewRectangle(10, 40)
fmt.Printf("Square's area is %v\n", s.Area())
type Rectangle struct {
a int
b int
}
func NewRectangle(a, b int) *Rectangle {
return &Rectangle{a: a, b: b}
}
func (s *Rectangle) Area() int {
@kadukf-outreach
kadukf-outreach / example_embedded_type_2.go
Created September 14, 2020 21:27
Defining and using embedded type
type Shape interface {
Area() int
}
type Square struct {
Shape
a int
}
func NewSquare(a int) *Square {
@kadukf-outreach
kadukf-outreach / example_embedded_type.go
Last active September 15, 2020 11:22
Embedded type w/o implementation
package main
import (
"fmt"
)
type Shape interface {
Area() int
}
@kadukf-outreach
kadukf-outreach / example_interface.go
Last active September 15, 2020 11:16
Interface and its implementation (golang)
type Shape interface {
Area() int
}
type Square struct {
a int
}
func NewSquare(a int) *Square {
return &Square{a: a}