looking at Go from a Python OOP’s perspective

tags: learning go programming

content

  • a typical python class:
class Person:
	def __init__(self, name, age):
		self.name = name
		self.age = age
	def talk(self):
		print(f"i am {self.name}")
  • how would this be implemented in Go?
type Person struct {
	name, string
	age,  int
}
func NewPerson(name string, age int) *Person {
	return &Person{
		name,
		age,
	}
}
func (p Person)talk() {
	fmt.Printf("i am $s", p.name)
}

up

down

reference