Golang: Structs collection of fields properties


Explanation regarding Struct in Golang:

  1. A struct is a collection of fields/properties.
    type Bootcamp struct {
        // Latitude of the event
        Lat float64
        // Longitude of the event
        Lon float64
        // Date of the event
        Date time.Time
    }
    
  2. You can define new types as structs or interfaces
    type Bootcamp struct {
        // Latitude of the event
        Lat float64
        // Longitude of the event
        Lon float64
        // Date of the event
        Date time.Time
    }
    
    type Namer interface {
        Name() string
    }
    
    type Reader interface {
        Read(b []byte) (n int, err error)
    }
    
    type Writer interface {
        Write(b []byte) (n int, err error)
    }
    
    type ReadWriter interface {
        Reader
        Writer
    }
    
    
  3. you can think of a struct to be a light class that supports composition but not inheritance.
    package main
    
    import "fmt"
    
    type User struct {
        Id    int
        Name, Location string
    }
    type Player struct {
        User
        GameId int
    }
    
    func main() {
        p := Player{
            User{Id: 42, Name: "Matt", Location: "LA"},
            90404,
        }
        fmt.Printf("Id: %d, Name: %s, Location: %s, Game id: %d\n",
            p.Id, p.Name, p.Location, p.GameId)
        // Directly set a field defined on the Player struct
        p.Id = 11
        fmt.Printf("%+v", p)
    }
    
  4. You don’t need to define getters and setters on struct fields, they can be accessed automatically.
    event := Bootcamp{
        Lat: 34.012836,
        Lon: -118.495338,
    }
    event.Date = time.Now()
    fmt.Printf("Event on %s, location (%f, %f)", event.Date, event.Lat, event.Lon)
    
  5. However, note that only exported fields (capitalized) can be accessed from outside of a package.
  6. A struct literal sets a newly allocated struct value by listing the values of its fields.
  7. You can list just a subset of fields by using the “Name:” syntax (the order of named fields is irrelevant when using this syntax).
     event := Bootcamp{
        Lat: 34.012836,
        Lon: -118.495338,
    }
  8. The special prefix & constructs a pointer to a newly allocated struct.

COMPOSITION

Golang Composition Sample Code 1:

package main

import "fmt"

type User struct {
	Id       int
	Name     string
	Location string
}

type Player struct {
	User
	Location string
	GameId   int
}

func main() {
	p := Player{}
	p.Id = 42
	p.Name = "Matt"
	p.Location = "LA"
	p.GameId = 90404
	fmt.Printf("%+v", p)
}

Golang Composition Sample Code 2 (The other option is to use a Google Language struct literal:) :

package main

import "fmt"

type User struct {
	Id             int
	Name, Location string
}

type Player struct {
	User
	GameId uint
}

func main() {
	p := Player{
		User{Id: 42, Name: "Matt", Location: "LA"},
		9004,
	}
	fmt.Printf("%+v", p)
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s