Skip to content

Commit b9ae4ff

Browse files
hpstorykrahets
andauthored
feature: add auto-build-and-test workflow for go (#1019)
* fix(csharp): unified array statement * feature: add workflow for go/js/ts/zig * fix python UnicodeDecodeError on windows * Update space_complexity.go * Update space_complexity_test.go * Update space_complexity.go * remove nodejs, zip workflow --------- Co-authored-by: Yudong Jin <krahets@163.com>
1 parent d85a3bb commit b9ae4ff

File tree

4 files changed

+51
-35
lines changed

4 files changed

+51
-35
lines changed

.github/workflows/go.yml

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
name: Go
2+
3+
on:
4+
push:
5+
branches: [ "main" ]
6+
paths: ["codes/go/**/*.go"]
7+
pull_request:
8+
branches: [ "main" ]
9+
paths: ["codes/go/**/*.go"]
10+
workflow_dispatch:
11+
12+
jobs:
13+
build:
14+
name: Go ${{ matrix.go-version }} on ${{ matrix.os }}
15+
runs-on: ${{ matrix.os }}
16+
defaults:
17+
run:
18+
working-directory: codes/go/
19+
strategy:
20+
matrix:
21+
os: [ubuntu-latest, macos-latest, windows-latest]
22+
go-version: ["1.19.x"]
23+
24+
steps:
25+
- uses: actions/checkout@v4
26+
27+
- name: Setup Go ${{ matrix.go-version }}
28+
uses: actions/setup-go@v3
29+
with:
30+
go-version: ${{ matrix.go-version }}
31+
- name: Check out code into the Go module directory
32+
run: go get -v -t -d ./...
33+
- name: Build
34+
run: go build -v ./...
35+
- name: Test with Go
36+
run: go test -v ./...

codes/go/chapter_computational_complexity/space_complexity.go

Lines changed: 11 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ package chapter_computational_complexity
77
import (
88
"fmt"
99
"strconv"
10+
11+
. "github.com/krahets/hello-algo/pkg"
1012
)
1113

1214
/* 结构体 */
@@ -15,33 +17,11 @@ type node struct {
1517
next *node
1618
}
1719

18-
/* treeNode 二叉树 */
19-
type treeNode struct {
20-
val int
21-
left *treeNode
22-
right *treeNode
23-
}
24-
2520
/* 创建 node 结构体 */
2621
func newNode(val int) *node {
2722
return &node{val: val}
2823
}
2924

30-
/* 创建 treeNode 结构体 */
31-
func newTreeNode(val int) *treeNode {
32-
return &treeNode{val: val}
33-
}
34-
35-
/* 输出二叉树 */
36-
func printTree(root *treeNode) {
37-
if root == nil {
38-
return
39-
}
40-
fmt.Println(root.val)
41-
printTree(root.left)
42-
printTree(root.right)
43-
}
44-
4525
/* 函数 */
4626
func function() int {
4727
// 执行某些操作...
@@ -54,7 +34,7 @@ func spaceConstant(n int) {
5434
const a = 0
5535
b := 0
5636
nums := make([]int, 10000)
57-
ListNode := newNode(0)
37+
node := newNode(0)
5838
// 循环中的变量占用 O(1) 空间
5939
var c int
6040
for i := 0; i < n; i++ {
@@ -64,7 +44,10 @@ func spaceConstant(n int) {
6444
for i := 0; i < n; i++ {
6545
function()
6646
}
67-
fmt.Println(a, b, nums, c, ListNode)
47+
b += 0
48+
c += 0
49+
nums[0] = 0
50+
node.val = 0
6851
}
6952

7053
/* 线性阶 */
@@ -112,12 +95,12 @@ func spaceQuadraticRecur(n int) int {
11295
}
11396

11497
/* 指数阶(建立满二叉树) */
115-
func buildTree(n int) *treeNode {
98+
func buildTree(n int) *TreeNode {
11699
if n == 0 {
117100
return nil
118101
}
119-
root := newTreeNode(0)
120-
root.left = buildTree(n - 1)
121-
root.right = buildTree(n - 1)
102+
root := NewTreeNode(0)
103+
root.Left = buildTree(n - 1)
104+
root.Right = buildTree(n - 1)
122105
return root
123106
}

codes/go/chapter_computational_complexity/space_complexity_test.go

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,21 @@ package chapter_computational_complexity
66

77
import (
88
"testing"
9+
10+
. "github.com/krahets/hello-algo/pkg"
911
)
1012

1113
func TestSpaceComplexity(t *testing.T) {
12-
/* ======= Test Case ======= */
1314
n := 5
14-
15-
/* ====== Driver Code ====== */
1615
// 常数阶
1716
spaceConstant(n)
18-
1917
// 线性阶
2018
spaceLinear(n)
2119
spaceLinearRecur(n)
22-
2320
// 平方阶
2421
spaceQuadratic(n)
2522
spaceQuadraticRecur(n)
26-
2723
// 指数阶
2824
root := buildTree(n)
29-
printTree(root)
25+
PrintTree(root)
3026
}

codes/python/test_all.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
stderr=subprocess.PIPE,
1919
text=True,
2020
env=env,
21+
encoding='utf-8'
2122
)
2223
# Wait for the process to complete, and get the output and error messages
2324
stdout, stderr = process.communicate()

0 commit comments

Comments
 (0)