Gauntlet Documentation
  • 🏠Welcome
  • πŸ…Getting Started
    • 🧠Introduction To Gauntlet
  • ‼️Read before Proceeding
  • ⬇️Installation
  • πŸ‘¨β€πŸ’»VSCode Extension
  • πŸ“šBasics
    • πŸ’¨Running Gauntlet
    • πŸ“„File Setup & Hello World
  • πŸ” Scope Variables
  • πŸ–ΌοΈConstants
  • 🧩Functions
  • ↔️If Statements
  • πŸ”‘Ternary Operator
  • πŸ’ Switch-Case
  • πŸ“©Select-Case
  • ➰Loops
  • πŸ“Structs
  • 🧱Interfaces
  • πŸͺͺAliases
  • πŸ“ŽMethods
  • πŸ¦™Lambdas
  • πŸ•ΈοΈMiscellaneous
  • ⚑Advanced Features
    • πŸ”€When-Cases
    • 🚰Pipes
    • ⁉️Try-Statements
    • 🎭Force-Statements
    • 🌯Wrapper Types
Powered by GitBook
On this page
  • Struct Declaration Syntax
  • Struct Construction
  • Working Example
Export as PDF

Structs

PreviousLoopsNextInterfaces

Last updated 8 days ago

Struct Declaration Syntax

[export] struct <StructName>[Generics] {
  [export] <FieldType> <FieldName> [`FieldTag`] // Syntax of fields
  <EmbeddedInterfaces> // Syntax of embedded interfaces
  <EmbeddedStructs> // Syntax of embedded structs
}

For syntax of Generics -

Struct Construction

<StructName>[GenericsInstantiation]{Field1 = Value1, etc}

Working Example

Embedded fields can only be accessed off of the embedded type. They are not a part of the "overall" type. For instance, if B with field C is embedded into A, then C can only be accessed by A.B.C, not A.C.

While generics were not needed in this specific example, they were used to showcase syntax

package main

import "fmt" as fmt
import "golang.org/x/exp/constraints" as constraints

struct BankAccount[constraints.Integer T] {
  T Retirement
  T Checking
  T Total
}

struct Human[constraints.Integer T] {
  export String FullName
  export Int Age
  BankAccount[T]
}


fun Unit main() {
  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)

}
package main

import fmt "fmt"
import constraints "golang.org/x/exp/constraints"

type bankAccount[T constraints.Integer] struct {
	total      T
	checking   T
	retirement T
}

type human[T constraints.Integer] struct {
	Age      int
	FullName string

	bankAccount[T]
}

func main() {
	age := 28
	firstName := "John"
	lastName := "Doe"
	fullName := firstName + " " + lastName
	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)
	// Eliminates any 'unused variable' errors
	_, _, _, _, _ = age, firstName, fullName, john, lastName

}
John's full name is:
John Doe
In total, he has this much money:
101000

πŸ“
Common placeholders throughout documentation