πStructs
Struct Declaration Syntax
[export] struct <StructName>[Generics] {
[export] <FieldName>: <FieldType> [`FieldTag`] // Syntax of fields
<EmbeddedInterfaces> // Syntax of embedded interfaces
<EmbeddedStructs> // Syntax of embedded structs
}
For syntax of Generics
- Common placeholders throughout documentation
Struct Construction
<StructName>[GenericsInstantiation]{Field1 = Value1, etc}
Working Example
package main
import "fmt" as fmt
import "golang.org/x/exp/constraints" as constraints
struct BankAccount[T : constraints.Integer] {
Retirement: T
Checking: T
Total: T
}
struct Human[T: constraints.Integer] {
export FullName: String
export Age: Int
BankAccount[T]
}
fun main(): Unit {
let age = 28
let firstName = "John"
let lastName = "Doe"
let fullName = firstName + " " + lastName
let john = Human[Int]{FullName = fullName, Age = age, BankAccount = BankAccount[Int]{Retirement = 100000, Checking = 1000, Total = 100000 + 1000}}
fmt.println("John's full name is:")
fmt.println(john.FullName)
fmt.println("In total, he has this much money:")
fmt.println(john.BankAccount.Total)
}
Last updated