MCPcopy Index your code
hub / github.com/Shopify/go-lua / ExampleFunction

Function ExampleFunction

doc_test.go:8–22  ·  view source on GitHub ↗

This example receives a variable number of numerical arguments and returns their average and sum.

(l *lua.State)

Source from the content-addressed store, hash-verified

6
7// This example receives a variable number of numerical arguments and returns their average and sum.
8func ExampleFunction(l *lua.State) int {
9 n := l.Top() // Number of arguments.
10 var sum float64
11 for i := 1; i <= n; i++ {
12 f, ok := l.ToNumber(i)
13 if !ok {
14 l.PushString("incorrect argument")
15 l.Error()
16 }
17 sum += f
18 }
19 l.PushNumber(sum / float64(n)) // First result.
20 l.PushNumber(sum) // Second result.
21 return 2 // Result count.
22}

Callers

nothing calls this directly

Calls 5

TopMethod · 0.80
ToNumberMethod · 0.80
PushStringMethod · 0.80
PushNumberMethod · 0.80
ErrorMethod · 0.45

Tested by

no test coverage detected