google / starlark-go

Starlark in Go: the Starlark configuration language, implemented in Go
BSD 3-Clause "New" or "Revised" License
2.26k stars 204 forks source link

Documentation on how to use modules #555

Open jayaprabhakar opened 1 month ago

jayaprabhakar commented 1 month ago

I see a number of modules defined in https://github.com/google/starlark-go/tree/master/starlark/testdata, But I am not sure how to use those modules, I'm primarily looking for the math module.

Similarly, I see another math module in the starlark-go code: https://github.com/google/starlark-go/tree/master/lib/math Again, I couldn't find any documentation how to use it.

It would be also be nice, to have at least one example on how to use starlarkstruct.Module to define a new module and use them as well.

jayaprabhakar commented 1 month ago

I found how to use the modules defined in go using starlarkstruct.Module.

For example: To use the math module defined in https://github.com/google/starlark-go/tree/master/lib/math just add it to the globals.

globals := starlark.StringDict{
    "math":  math.Module,
}

Then, in the starlark code, you can simply use,

print(math.ceil(3.5))