1 2<!-- This file is the project homepage for go.starlark.net --> 3 4# Starlark in Go 5 6[](https://travis-ci.org/google/starlark-go) 7[](https://godoc.org/go.starlark.net/starlark) 8 9This is the home of the _Starlark in Go_ project. 10Starlark in Go is an interpreter for Starlark, implemented in Go. 11Starlark was formerly known as Skylark. 12The new import path for Go packages is `"go.starlark.net/starlark"`. 13 14Starlark is a dialect of Python intended for use as a configuration language. 15Like Python, it is an untyped dynamic language with high-level data 16types, first-class functions with lexical scope, and garbage collection. 17Unlike CPython, independent Starlark threads execute in parallel, so 18Starlark workloads scale well on parallel machines. 19Starlark is a small and simple language with a familiar and highly 20readable syntax. You can use it as an expressive notation for 21structured data, defining functions to eliminate repetition, or you 22can use it to add scripting capabilities to an existing application. 23 24A Starlark interpreter is typically embedded within a larger 25application, and the application may define additional domain-specific 26functions and data types beyond those provided by the core language. 27For example, Starlark was originally developed for the 28[Bazel build tool](https://bazel.build). 29Bazel uses Starlark as the notation both for its BUILD files (like 30Makefiles, these declare the executables, libraries, and tests in a 31directory) and for [its macro 32language](https://docs.bazel.build/versions/master/skylark/language.html), 33through which Bazel is extended with custom logic to support new 34languages and compilers. 35 36 37## Documentation 38 39* Language definition: [doc/spec.md](doc/spec.md) 40 41* About the Go implementation: [doc/impl.md](doc/impl.md) 42 43* API documentation: [godoc.org/go.starlark.net/starlark](https://godoc.org/go.starlark.net/starlark) 44 45* Mailing list: [starlark-go](https://groups.google.com/forum/#!forum/starlark-go) 46 47* Issue tracker: [https://github.com/google/starlark-go/issues](https://github.com/google/starlark-go/issues) 48 49### Getting started 50 51Build the code: 52 53```shell 54# check out the code and dependencies, 55# and install interpreter in $GOPATH/bin 56$ go get -u go.starlark.net/cmd/starlark 57``` 58 59Run the interpreter: 60 61```console 62$ cat coins.star 63coins = { 64 'dime': 10, 65 'nickel': 5, 66 'penny': 1, 67 'quarter': 25, 68} 69print('By name:\t' + ', '.join(sorted(coins.keys()))) 70print('By value:\t' + ', '.join(sorted(coins.keys(), key=coins.get))) 71 72$ starlark coins.star 73By name: dime, nickel, penny, quarter 74By value: penny, nickel, dime, quarter 75``` 76 77Interact with the read-eval-print loop (REPL): 78 79```pycon 80$ starlark 81>>> def fibonacci(n): 82... res = list(range(n)) 83... for i in res[2:]: 84... res[i] = res[i-2] + res[i-1] 85... return res 86... 87>>> fibonacci(10) 88[0, 1, 1, 2, 3, 5, 8, 13, 21, 34] 89>>> 90``` 91 92When you have finished, type `Ctrl-D` to close the REPL's input stream. 93 94Embed the interpreter in your Go program: 95 96```go 97import "go.starlark.net/starlark" 98 99// Execute Starlark program in a file. 100thread := &starlark.Thread{Name: "my thread"} 101globals, err := starlark.ExecFile(thread, "fibonacci.star", nil, nil) 102if err != nil { ... } 103 104// Retrieve a module global. 105fibonacci := globals["fibonacci"] 106 107// Call Starlark function from Go. 108v, err := starlark.Call(thread, fibonacci, starlark.Tuple{starlark.MakeInt(10)}, nil) 109if err != nil { ... } 110fmt.Printf("fibonacci(10) = %v\n", v) // fibonacci(10) = [0, 1, 1, 2, 3, 5, 8, 13, 21, 34] 111``` 112 113See [starlark/example_test.go](starlark/example_test.go) for more examples. 114 115### Contributing 116 117We welcome submissions but please let us know what you're working on 118if you want to change or add to the Starlark repository. 119 120Before undertaking to write something new for the Starlark project, 121please file an issue or claim an existing issue. 122All significant changes to the language or to the interpreter's Go 123API must be discussed before they can be accepted. 124This gives all participants a chance to validate the design and to 125avoid duplication of effort. 126 127Despite some differences, the Go implementation of Starlark strives to 128match the behavior of [the Java implementation](https://github.com/bazelbuild/bazel) 129used by Bazel and maintained by the Bazel team. 130For that reason, proposals to change the language itself should 131generally be directed to [the Starlark site]( 132https://github.com/bazelbuild/starlark/), not to the maintainers of this 133project. 134Only once there is consensus that a language change is desirable may 135its Go implementation proceed. 136 137We use GitHub pull requests for contributions. 138 139Please complete Google's contributor license agreement (CLA) before 140sending your first change to the project. If you are the copyright 141holder, you will need to agree to the 142[individual contributor license agreement](https://cla.developers.google.com/about/google-individual), 143which can be completed online. 144If your organization is the copyright holder, the organization will 145need to agree to the [corporate contributor license agreement](https://cla.developers.google.com/about/google-corporate). 146If the copyright holder for your contribution has already completed 147the agreement in connection with another Google open source project, 148it does not need to be completed again. 149 150### Stability 151 152We reserve the right to make breaking language and API changes at this 153stage in the project, although we will endeavor to keep them to a minimum. 154Once the Bazel team has finalized the version 1 language specification, 155we will be more rigorous with interface stability. 156 157### Credits 158 159Starlark was designed and implemented in Java by 160Ulf Adams, 161Lukács Berki, 162Jon Brandvein, 163John Field, 164Laurent Le Brun, 165Dmitry Lomov, 166Damien Martin-Guillerez, 167Vladimir Moskva, and 168Florian Weikert, 169standing on the shoulders of the Python community. 170The Go implementation was written by Alan Donovan and Jay Conrod; 171its scanner was derived from one written by Russ Cox. 172 173### Legal 174 175Starlark in Go is Copyright (c) 2018 The Bazel Authors. 176All rights reserved. 177 178It is provided under a 3-clause BSD license: 179[LICENSE](https://github.com/google/starlark-go/blob/master/LICENSE). 180 181Starlark in Go is not an official Google product. 182