Skip to content

Commit

Permalink
chore: update KCL Go SDK blogs
Browse files Browse the repository at this point in the history
Signed-off-by: peefy <[email protected]>
  • Loading branch information
Peefy committed Mar 14, 2024
1 parent 2736e6f commit 8913f62
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 173 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,25 @@ KCL is closely related to the cloud-native domain as a configuration language, w

The initial version of the KCL compiler and runtime were written in Python, and the runtime for the first version of the KCL language had a lot of room for improvement in terms of performance and security due to the performance issues and characteristics of the dynamic nature of the Python language. In light of security and efficiency considerations, later versions of the KCL compiler were written in the Rust programming language. As a result, the new version of `KCL Go SDK` is based on rust-implemented kclvm packaging, eliminating Python dependencies, simplifying installation, and optimizing the user experience.

## Quickly experience KCL Go SDK via the command line
## How to integrate KCL with Go code?

`KCL Go SDK` provides a built-in command line tool named `kcl-go` ,which supports one-click installation through `go install`. The local Go version must be 1.18+ and the complete CGO toolchain is required.
Here is an example of how to integrate KCL into your Go program. Using the hello.k file from the previous example, construct the following main.go code:

Simply run:
```go
package main

```bash
go install kusionstack.io/kclvm-go/cmds/kcl-go@latest
```
import (
"fmt"

Create a new KCL source file hello.k
kcl "kcl-lang.io/kcl-go"
)

func main() {
yaml := kcl.MustRun("kubernetes.k", kcl.WithCode(code)).GetRawYamlResult()
fmt.Println(yaml)
}

```python
const code = `
apiVersion = "apps/v1"
kind = "Deployment"
metadata = {
Expand All @@ -54,98 +60,11 @@ spec = {
}
]
}
`
```

And then execute the KCL directly from the command line with:

```shell
kcl-go run ./hello.k
```

The output is

```yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx
labels:
app: nginx
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: "nginx:1.14.2"
ports:
- containerPort: 80
```
## How to integrate KCL with Go code?
Here is an example of how to integrate KCL into your Go program. Using the hello.k file from the previous example, construct the following main.go code:
```go
package main

import (
"fmt"
"kusionstack.io/kclvm-go"
)

func main() {
result := kclvm.MustRun("./hello.k").GetRawYamlResult()
fmt.Println(result)
}
```

- `kclvm.MustRun("./hello.k").GetRawYamlResult()` runs the corresponding KCL source file
- `fmt.Println(result)` prints the result of the run

The local environment requires Go version 1.18+ and a complete CGO toolchain. Add the `KCL Go SDK` dependency to this command line tool by running:

```bash
go get kusionstack.io/kclvm-go@main
```

The following command runs the Go program:

```shell
go run main.go
```

The output is

```yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx
labels:
app: nginx
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: "nginx:1.14.2"
ports:
- containerPort: 80
```
- `kcl.MustRun("kubernetes.k", kcl.WithCode(code)).GetRawYamlResult()` runs the corresponding KCL source file
- `fmt.Println(yaml)` prints the result of the run

## Conclusion

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,25 @@ KCL 作为一门配置型语言,和云原生领域有着极其紧密的联系

新版本`KCL Go SDK`可以视为一个纯 Go 包使用,无需任何外置依赖,可以通过一键`go install`即可完成安装使用。

## 命令行 `KCL Go SDK`快速体验
## Go 代码如何集成 KCL

`KCL Go SDK`提供了一个自带的 KCL Go 命令行,支持用户通过`go install`来一键安装 kclvm 的 Go 命令行工具 `kcl-go`,其要求本地 Go 版本为1.18+, 同时要求本地有完整的 CGO 工具链。
以上一节的 hello.k 为例,构建以下的 main.go 代码:

只需执行
```go
package main

```bash
go install kusionstack.io/kclvm-go/cmds/kcl-go@latest
```
import (
"fmt"

新建 KCL 源文件 hello.k
kcl "kcl-lang.io/kcl-go"
)

```python
func main() {
yaml := kcl.MustRun("kubernetes.k", kcl.WithCode(code)).GetRawYamlResult()
fmt.Println(yaml)
}

const code = `
apiVersion = "apps/v1"
kind = "Deployment"
metadata = {
Expand All @@ -56,75 +62,11 @@ spec = {
}
]
}
`
```

之后可以直接在命令行中执行 KCL

```shell
$ kcl-go run ./hello.k
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx
labels:
app: nginx
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: "nginx:1.14.2"
ports:
- containerPort: 80
```

## Go 代码如何集成 KCL

以上一节的 hello.k 为例,构建以下的 main.go 代码:

```go
package main

import (
"fmt"
"kusionstack.io/kclvm-go"
)

func main() {
result := kclvm.MustRun("./hello.k").GetRawYamlResult()
fmt.Println(result)
}
```

- `kclvm.MustRun("./hello.k").GetRawYamlResult()`运行对应的kcl源文件
- `fmt.Println(result)`打印运行结果

本地环境要求 Go 版本为1.18+,与完整的 CGO 工具链。运行命令行添加 `KCL Go SDK`依赖

```bash
go get kusionstack.io/kclvm-go@main
```

执行 Go 程序,结果为:

```shell
$ go run main.go
name: kcl
age: 1
x0:
name: kcl
age: 1
x1:
name: kcl
age: 101
```
- `kcl.MustRun("kubernetes.k", kcl.WithCode(code)).GetRawYamlResult()`运行对应的kcl源文件
- `fmt.Println(yaml)`打印运行结果

## 总结

Expand Down

0 comments on commit 8913f62

Please sign in to comment.