Class (Methods)

I can't believe when sentdex tell me that Golang doesn't have class methods.

But soon I know what's the big idea under the mystery.

Python Version

class box():
    def __init__(self, length, width, hight):
        self.length = length
        self.width = width
        self.hight = hight

    def get_volume(self):
        return self.length * self.width * self.hight
    
    def change_length(self):
        self.length = length // 2

box_a = box(3, 2, 1)
box_b = box(4, 2, 2)
print('The volume of box_a is', box_a.get_volume())
print('The volume of box_b is', box_b.get_volume())

Golang Version

This is the basic way for class method.

You can also pass pointer to class function:

Last updated

Was this helpful?