DEV Community

Cover image for Class in Go
bluepaperbirds
bluepaperbirds

Posted on

Class in Go

Go is not a pure object oriented programming language. In truth, Go does not provide classes. Go uses structs instead.

Create a file example.go with the struct student. You can create a new person using this struct as the example shows:

package main

import "fmt"

type student struct {
    FirstName       string
    LastName        string
}

func main() {
    s1 := student{ FirstName: "Nils", LastName: "Bohr" }
    fmt.Println(s1.FirstName)
}

This program outputs in:

➜  go run app.go   
Nils

You can create multiple students like this:

s1 := student{ FirstName: "Nils", LastName: "Bohr" }
s2 := student{ FirstName: "Albert", LastName: "Einstein" }
fmt.Println(s1.FirstName)
fmt.Println(s2.FirstName)

Methods

So what about methods? Methods can be "added to structs". You can create a method that takes a struct as parameter, like this:

func (s student) name() {
    fmt.Printf("%s %s\n", s.FirstName, s.LastName)
}

Then you can call it like this:

s1.name()
s2.name()

That makes the program:

package main

import "fmt"

type student struct {
    FirstName       string
    LastName        string
}

func (s student) name() {
    fmt.Printf("%s %s\n", s.FirstName, s.LastName)
}

func main() {
    s1 := student{ FirstName: "Nils", LastName: "Bohr" }
    s2 := student{ FirstName: "Albert", LastName: "Einstein" }
    s1.name()
    s2.name()
}

Related links:

Top comments (0)