DEV Community

Pierangelo
Pierangelo

Posted on

3 2

using superheroes for refresh my mind about Golang structs

After have followed an amazing course about golang one years ago, I had need to refresh my mind about the Struct in golang, and for do it I had used some superheroes:

First Example:

package main

import "fmt"

type skill struct {
    Power string
}

type hero struct {
    Name  string
    Alias string
    skill
}

func main() {
    h1 := hero{
        Name:  "Bruce Wayne",
        Alias: "Batman",
        skill: skill{
            Power: "a lot of gadgets",
        },
    }
    h2 := hero{
        Name:  "Tony Stark",
        Alias: "Iron",
        skill: skill{
            Power: "hi-tech armor",
        },
    }

    fmt.Printf("the best skill of %s is %s \n", h1.Alias, h1.skill.Power)
    fmt.Printf("the best skill of %s is %s \n", h2.Alias, h2.skill.Power)

    s1 := skill{
        Power: "velocity",
    }

    h3 := hero{
        Name:  "Barry Allen",
        Alias: "Flash",
        skill: skill{
            Power: s1.Power,
        },
    }

    fmt.Printf("the best skill of %s is %s \n", h3.Alias, h3.skill.Power)
}
Enter fullscreen mode Exit fullscreen mode

Second Example

package main

import "fmt"

type skill struct {
    Power string
}

type hero struct {
    Name  string
    Alias string
}

type heroSkill struct {
    hero
    skill
}

func main() {
    hs1 := heroSkill{
        hero: hero{
            Name:  "Clark Kent / Kal-El",
            Alias: "Superman",
        },
        skill: skill{
            Power: "a lot of cool superpowers",
        },
    }

    hs2 := heroSkill{
        hero: hero{
            Name:  "Steve Rogers",
            Alias: "Capitan America",
        },
        skill: skill{
            Power: "strong",
        },
    }

    fmt.Printf("the best skill of %s is %s \n", hs1.hero.Alias, hs1.skill.Power)
    // check the different way to call the struct, in this second Print, there no need to call the sub struct.
    fmt.Printf("the best skill of %s is %s \n", hs2.Alias, hs2.Power)
}

Enter fullscreen mode Exit fullscreen mode

Image of Datadog

Create and maintain end-to-end frontend tests

Learn best practices on creating frontend tests, testing on-premise apps, integrating tests into your CI/CD pipeline, and using Datadog’s testing tunnel.

Download The Guide

Top comments (0)

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay