In Go programming language, we use structs to define custom data types. In order to create instances of these data types, we can use the new()
keyword.
Here's an example of creating an instance of a struct in Go:
main.go322 chars17 lines
In this example, we defined a struct Person
with two fields, name
and age
. We then created a new instance of this struct using the new()
keyword. Since new()
returns a pointer to the newly allocated zero value of the struct, we assigned the returned value to a variable p
.
We then assigned values to the fields of p
and printed them out.
Output:
main.go22 chars2 lines
gistlibby LogSnag