Skip to main content

Command Palette

Search for a command to run...

Struct Literal

Updated
1 min readView as Markdown
Struct Literal
S
DevRel at StackGen | Formerly at Deepfence ,Tenable , Accurics | AWS Community Builder also Docker Community Award Winner at Dockercon2020 | CyberSecurity Innovator of Year 2023 award by Bsides Bangalore | Docker/HashiCorp Meetup Organiser Bangalore & Co-Author of Learn Lightweight Kubernetes with k3s (2019) , Packt Publication & also run Non Profit CloudNativeFolks / CloudSecCorner Community To Empower Free Education reach out me twitterhttps://twitter.com/sangamtwts or just follow on GitHub -> https://github.com/sangam14 for Valuable Resources

A struct literal denotes a newly allocated struct value by listing the values of its fields. You can list just a subset of fields by using the Name: syntax. (And the order of named fields is irrelevant.) The special prefix & returns a pointer to the struct value.

var (
    z1 = xy{1, 2}  // has type xy. z1={1,2}
    z2 = xy{X: 1}  // Y:0 is implicit z2={1,0}
    z3 = xy{}      // X:0 and Y:0 z3={0,0}
    p  = &xy{1, 2} // has type *xy  p={1,2}
)

Go Playground

package main

import "fmt"

type xy struct {
    X, Y int
}

var (
    z1 = xy{1, 2}  // has type xy
    z2 = xy{X: 1}  // Y:0 is implicit
    z3 = xy{}      // X:0 and Y:0
    p  = &xy{1, 2} // has type *xy
)

func main() {
    fmt.Println(z1, p, z2, z3)
}
12 views

GopherLabs

Part 1 of 50

Ultimate Series of Blogs for All Golang Developer

More from this blog

C

CloudNativeFolks Community

140 posts

CloudNativeFolks is non profit community that empower and educate about cloud native technology !