https://github.com/ermos/strlang
🏗️ fluent string builder interface for generate code easily with go
https://github.com/ermos/strlang
Last synced: over 1 year ago
JSON representation
🏗️ fluent string builder interface for generate code easily with go
- Host: GitHub
- URL: https://github.com/ermos/strlang
- Owner: ermos
- License: mit
- Created: 2023-03-02T11:05:51.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2023-03-10T10:36:55.000Z (over 3 years ago)
- Last Synced: 2025-01-06T08:37:50.791Z (over 1 year ago)
- Language: Go
- Homepage:
- Size: 43 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# strlang
> 🏗️ fluent string builder interface for generate code easily with go
`strlang` is a Go package for building string-based programming languages.
It provides a simple way to generate strings of code that can be used
in different programming languages.
[](https://pkg.go.dev/github.com/ermos/strlang)
[](https://github.com/ermos/strlang/tags)
[](https://goreportcard.com/report/github.com/ermos/strlang)
[](https://codeclimate.com/github/ermos/strlang/maintainability)
[](https://codecov.io/github/ermos/strlang)
## 🛠️ Installation
To use `strlang` in your Go project, you need to have Go installed on your system.
Once you have Go installed, you can use the following command to install the package:
```go
go get -u github.com/ermos/strlang
```
## 📚 Examples
Here are some examples of how to use `strlang` to generate code in different programming languages.
If your wanted programming language isn't implemented, you can use directly the default builder.
### Javascript
Here is an example of how to use Strlang to generate JavaScript code:
#### Code
```go
package main
import (
"github.com/ermos/strlang"
"fmt"
)
func main() {
js := strlang.NewJavascript()
js.Object("const", "person", func() {
js.WriteStringln(`name: "John",`)
js.WriteStringln(`age: 31,`)
js.WriteStringln(`city: "New York"`)
})
js.If("person.age > 18", func() {
js.WriteStringln(`console.log("John is an adult");`)
})
js.Else(func() {
js.WriteStringln("console.log('John is not an adult');")
})
fmt.Println(js.String())
}
```
#### Output
```javascript
const person = {
name: "John",
age: 31,
city: "New York"
};
if (person.age > 18) {
console.log("John is an adult");
} else {
console.log("John is not an adult");
}
```
### PHP
Here is an example of how to use Strlang to generate PHP code:
#### Code
```go
package main
import (
"github.com/ermos/strlang"
"fmt"
)
func main() {
php := strlang.NewPHP("App/Models")
php.Class("Person", func() {
php.ClassFunc("public", "__construct", "$name, $age", "", func() {
php.WriteStringln(`$this->name = $name;`)
php.WriteStringln(`$this->age = $age;`)
})
})
fmt.Println(php.String())
}
```
#### Output
```php
name = $name;
$this->age = $age;
}
}
```
## 💡 Usage
`strlang` provides a set of builders for each programming language.
Each builder has methods that allow you to build code constructs in that language.
You can use the `New` method of your wanted language (example for javascript: `NewJavascript()`)
functions to create instances of the corresponding builders.
Once you have a builder instance, you can use its methods to build code constructs.
For more information about the available methods and how to use them, please refer to the package documentation.
## 🤝 Contributing
Contributions to `strlang` are always welcome!
If you find a bug or have a feature request, please open an issue on GitHub.
If you want to contribute code, please fork the repository and submit a pull request.