47 lines
1022 B
Plaintext
47 lines
1022 B
Plaintext
// Example: How to add builtins to the framework
|
|
// Just write a Go function and register it - that's all!
|
|
|
|
package main
|
|
|
|
import "fmt"
|
|
|
|
// Simple function - just register it!
|
|
func multiply(a, b int) int {
|
|
return a * b
|
|
}
|
|
|
|
// Function returning multiple values with error
|
|
func divide(a, b int) (int, error) {
|
|
if b == 0 {
|
|
return 0, fmt.Errorf("cannot divide by zero")
|
|
}
|
|
return a / b, nil
|
|
}
|
|
|
|
// Complex example with struct
|
|
type User struct {
|
|
Name string
|
|
Email string
|
|
Age int
|
|
}
|
|
|
|
func getUser(id int) (User, error) {
|
|
return User{
|
|
Name: "John Doe",
|
|
Email: "john@example.com",
|
|
Age: 30,
|
|
}, nil
|
|
}
|
|
|
|
// Register all builtins in init
|
|
// func init() {
|
|
// RegisterBuiltin("multiply", multiply)
|
|
// RegisterBuiltin("divide", divide)
|
|
// RegisterBuiltin("getUser", getUser)
|
|
// }
|
|
|
|
// That's it! TypeScript definitions are auto-generated:
|
|
// declare function multiply(arg0: number, arg1: number): number;
|
|
// declare function divide(arg0: number, arg1: number): number;
|
|
// declare function getUser(arg0: number): any;
|