Go
2009fragletsystemsimperativeconcurrent.go
docker run --rm --platform="linux/amd64" 100hellos/golang:latest
MCP + fragletc
MCPstdinargs
This language supports code execution via MCP and the fragletc CLI. Stdin piping and argument passing are both supported.
Install fragletc →Go is a systems imperative and concurrent language first appearing in 2009.
Hello World
package main
// BEGIN_FRAGLET
import (
"fmt"
)
func main() {
fmt.Println("Hello World!")
}
// END_FRAGLETCoding Guide
Language Version
Go 1.21
Execution Model
- Compiled language using
go build - Code is compiled to a binary, then executed
- Standard Go execution model with
main()function inmainpackage
Key Characteristics
- Statically typed
- Case-sensitive (exported identifiers start with uppercase)
- Garbage collected
- Strong typing with type inference (
:=operator) - Package-based module system
- Built-in concurrency primitives (goroutines, channels)
- No classes (uses structs and methods instead)
- Functions are first-class citizens
Fragment Authoring
Your fraglet is the full program below package main: include the import block and your code (functions, types, and main()). The only fixed line above the fraglet is package main. Your fraglet will be compiled and executed.
Available Packages
The template includes the standard library package:
fmt- Formatted I/O (Println, Printf, Sprintf)
Additional standard library packages can be imported as needed:
strings- String manipulationstrconv- String conversionsos- Operating system interfaceio- I/O primitivesmath- Mathematical functionstime- Time operationssync- Synchronization primitivesnet/http- HTTP client and serverencoding/json- JSON encoding/decoding- And many more from the Go standard library
Common Patterns
- Print:
fmt.Println("message")orfmt.Printf("format %s\n", value) - Variables:
var x int = 10orx := 10(type inference) - Functions:
func add(a, b int) int { return a + b } - Structs:
type Person struct { Name string; Age int } - Methods:
func (p Person) String() string { return fmt.Sprintf("%s (%d)", p.Name, p.Age) } - Slices:
numbers := []int{1, 2, 3}ornumbers := make([]int, 0, 10) - Maps:
m := make(map[string]int)orm := map[string]int{"a": 1} - Goroutines:
go func() { ... }() - Channels:
ch := make(chan int)
Examples
Each example is a complete fraglet (imports + code). package main sits above this content.
// Simple output
import (
"fmt"
)
func main() {
fmt.Println("Hello from fragment!")
}
// Variables and calculations
import (
"fmt"
)
func main() {
a := 5
b := 10
fmt.Printf("Sum: %d\n", a+b)
}
// Functions
import (
"fmt"
)
func add(a, b int) int {
return a + b
}
func main() {
result := add(5, 10)
fmt.Printf("5 + 10 = %d\n", result)
}
// Slices and loops
import (
"fmt"
)
func main() {
numbers := []int{1, 2, 3, 4, 5}
sum := 0
for _, num := range numbers {
sum += num
}
fmt.Printf("Sum: %d\n", sum)
}
// Structs and methods
import (
"fmt"
)
type Person struct {
Name string
Age int
}
func (p Person) String() string {
return fmt.Sprintf("%s is %d years old", p.Name, p.Age)
}
func main() {
p := Person{Name: "Alice", Age: 30}
fmt.Println(p)
}
// Maps
import (
"fmt"
)
func main() {
m := map[string]int{
"apple": 5,
"banana": 3,
}
fmt.Printf("Apples: %d\n", m["apple"])
}
// String operations
import (
"fmt"
)
func main() {
s := "Hello"
s += " World!"
fmt.Println(s)
fmt.Printf("Length: %d\n", len(s))
}Caveats
- Fragments must be valid Go code that compiles
- Remember that exported identifiers (functions, types, variables) must start with uppercase
- Variables declared with
:=must be used (Go doesn't allow unused variables) - The code is compiled fresh each time, so compilation errors will fail execution
- Go requires explicit error handling - functions that return errors should check them
- Slices are reference types, maps are reference types
- Zero values:
0for numbers,""for strings,nilfor pointers/slices/maps/channels
Fraglet Scripts
Echo Args
#!/usr/bin/env -S fragletc --vein=golang
import (
"fmt"
"os"
"strings"
)
func main() {
fmt.Println("Args:", strings.Join(os.Args[1:], " "))
}Go Fortune
#!/usr/bin/env -S fragletc --vein=golang
import (
"fmt"
)
func main() {
fmt.Println("Go: less is more. Then add generics.")
}Stdin Upper
#!/usr/bin/env -S fragletc --vein=golang
import (
"bufio"
"fmt"
"os"
"strings"
)
func main() {
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
fmt.Println(strings.ToUpper(scanner.Text()))
}
}Test
#!/usr/bin/env -S fragletc --vein=golang
import (
"fmt"
)
func main() {
fmt.Println("Hello World!")
}Connections
influenced by