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
  • Function Declaration Syntax
  • Working Example
Export as PDF

Functions

PreviousConstantsNextIf Statements

Last updated 8 days ago

Function Declaration Syntax

[exported] fun <ReturnType> <functionName>[Generics]([Parameters]) {
    <function body>
}

For syntax of Generics and Parameters -

Working Example

package main

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

fun Bool isOver[constraints.Integer T](T a, T b) {
  return a > b
} 

fun String createGreeting(String firstName, String lastName) {
  return "Hello " + firstName + " " + lastName + "!"
}

fun Unit main() {
  let age = 28
  let firstName = "John"
  let lastName = "Doe"
  let greeting = createGreeting(firstName, lastName)
  let isAdult = isOver(age, 18)
  fmt.println("Greeting:")
  fmt.println(greeting)
  fmt.println("Adult Status:")
  fmt.println(isAdult)
}

package main

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

func isOver[T constraints.Integer](a T, b T) bool {

	return a > b

}

func createGreeting(firstName string, lastName string) string {

	return "Hello " + firstName + " " + lastName + "!"

}

func main() {
	age := 28
	firstName := "John"
	lastName := "Doe"
	greeting := createGreeting(firstName, lastName)
	isAdult := isOver(age, 18)
	fmt.Println("Greeting:")
	fmt.Println(greeting)
	fmt.Println("Adult Status:")
	fmt.Println(isAdult)
	// Eliminates any 'unused variable' errors
	_, _, _, _, _ = age, firstName, greeting, isAdult, lastName

}
Greeting:
Hello John Doe!
Adult Status:
true

🧩
Common placeholders throughout documentation