#Beginner interface & function question

12 messages · Page 1 of 1 (latest)

tidal flint
#

Hello, I need some help understanding the code here:
https://github.com/iamshaunjp/golang-tutorials/blob/lesson-22/main.go
Its from this tutorial on interfaces https://www.youtube.com/watch?v=lbW-KVdIXaY

// square methods
func (s square) area() float64 {
    return s.length * s.length
}

Help me understand: So this is an anonymous function that takes parameter "s," which is of type struct named square and returns float64? What is the relevance of area() ? What is this doing in the function syntax? Are area() and circumf() part of the math package, because I don't see what they do or how it work in this code?

I don't see where the square methods and circle methods are being called. Really confused

Is shapes a slice of dictionaries?

GitHub

All course files for the Net Ninja Go tutorial series. - golang-tutorials/main.go at lesson-22 · iamshaunjp/golang-tutorials

In this final Golang tutorial of the series, we'll learn about interfaces!

🐱‍👤 View this course in full without ads on Net Ninja Pro:
https://netninja.dev/p/learn-go-golang

🐱‍💻 Course Files:

🐱‍👤 JOIN THE YOUTUBE NET NINJA GANG -
https://www.youtube.com/channel/UCW5YeuERMmlnqo4oq8vwUpg/join

🐱...

▶ Play video
mighty stone
#

it's a method

#

func (s square) area() float64 {
this syntax says that, the struct square assigned to variable s have a function called area which takes in nothing() and return float64

#

in the function itself, you can use s to refer to the instance of square which square.area has been called on

#

I don't see where the square methods and circle methods are being called. Really confused
it's being called by printShapeInfo(s shape) function

#

which takes a shape interface, and calls area and circumf

#

the main creates a slice of shapes storing shape interface, with circle and square which both types that complies with the interface

    shapes := []shape{
        square{length: 15.2},
        circle{radius: 7.5},
        circle{radius: 12.3},
        square{length: 4.9},
    }
#

which is iterated and passed to print info

    for _, v := range shapes {
        printShapeInfo(v)
        fmt.Println("---")
    }
#

shapes a slice of dictionaries?
not sure what you mean by that
there's no concept of dict in go, and i dont know other languages which do have that

#

shapes is a slice which holds a list of things that complies with the interface shape

tidal flint
#

Ok, I think I understand now. So with shapes, we have a slice of type shape interfaces, and the elements in the slice are all structs of name square or circle. These structs (square and circle) belong to the "shape" interface family.

 func (s square) area() float64 {
    return s.length * s.length
}```
The above code is 1) defining a method named `area()` and 2) Assigning the `area()` method to the square struct, correct? 

We iterate over the shapes and call `printShapeInfo `for each element. This is able to calls the method on each shape in the printf statement. 

So the idea was to add the structs to a family (interface) so that they could iterate nicely with the `printShapeInfo `function? There is no other way to have the function take both structs? And is this the best practice way for adding methods to a struct? Yes that syntax really confused me. 

Thanks @mighty stone , I want to ask if you or any senior Golang engineers here are available for 1 on 1 mentoring. I am looking to accelerate my learning speed. I had originally started with programming JS but my first role (1 year now) turned out to be working as an infrastructure engineer for customers on AWS Cloud with Terraform and I haven't grown much in the software engineering department -  I want to get my programming skills from jr to at least mid level. Please DM me if you have availability or are interested in mentoring.