Skip to content

Commit da309e9

Browse files
committed
✨ (sum_test): add test first
0 parents  commit da309e9

File tree

5 files changed

+138
-0
lines changed

5 files changed

+138
-0
lines changed

.gitignore

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# If you prefer the allow list template instead of the deny list, see community template:
2+
# https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore
3+
#
4+
# Binaries for programs and plugins
5+
*.exe
6+
*.exe~
7+
*.dll
8+
*.so
9+
*.dylib
10+
11+
# Test binary, built with `go test -c`
12+
*.test
13+
14+
# Output of the go coverage tool, specifically when used with LiteIDE
15+
*.out
16+
17+
# Dependency directories (remove the comment below to include it)
18+
# vendor/
19+
20+
# Go workspace file
21+
go.work
22+
go.work.sum
23+
24+
# env file
25+
.env
26+
bin

Makefile

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
.PHONY=build
2+
# include .env
3+
# export $(shell sed 's/=.*//' .env)
4+
5+
coverage:
6+
@go test -v -cover ./...
7+
8+
test:
9+
@go test -v ./...

README.md

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
2+
# Arrays 與 Slices
3+
4+
在 golang 中,陣列類型資料類型主要有兩種 array 與 slice 。
5+
6+
## array
7+
array 是一種固定長度的資料陣列。在初始化時,就設定好長度,並且無法後續修改。
8+
9+
```golang=
10+
arr := [4]int{1,1,1,1}
11+
arr := [...]int{1,1,1,1}
12+
```
13+
14+
![image](https://hackmd.io/_uploads/HkY6BHIbex.png)
15+
16+
## slice
17+
18+
slice 是則是一種動態長度的資料陣列。在初始化,可以設定好容量 capacity 。可以透過 append 新增資料到 slice 上。而實際上, slice 內部是指向一個 array 資料型態的參考,遇到超過預設 capacity 的資料量時,就會產生新的二倍於原本 capacity 的陣列實體,然後把 slice 參考更新。
19+
20+
```golang=
21+
slice := []int{1,1,1,1}
22+
// slice 的預設的 capcity 會是當下資料量長度,當不指定時
23+
// slice 基本有3個屬性 ptr 指向目前資料陣列參照, len 代表當下資料長度, cap 代表當下最多容量。
24+
```
25+
26+
![image](https://hackmd.io/_uploads/rJixLSI-xg.png)
27+
28+
# iteration
29+
30+
```golang=
31+
stockAmountByDay := []int{200,210, 300,311}
32+
```
33+
## loop over by index
34+
35+
```golang=
36+
sliceLength := len(stockAmountByDay)
37+
for idx := 0; idx < sliceLength; idx++ {
38+
stockAmount := stockAmountByDay[idx]
39+
fmt.Printf("stockAmount = %d\n",stockAmount)
40+
}
41+
```
42+
[iteration-slice-by-idx](https://go.dev/play/p/w_iYwtB6u6c)
43+
44+
## loop over by range
45+
46+
```golang=
47+
for _, stockAmount := range stockAmountByDay {
48+
fmt.Printf("stockAmount = %d\n",stockAmount)
49+
}
50+
```
51+
52+
[iteration-slice-by-range](https://go.dev/play/p/CYLQDp2p4mY)
53+
54+
# operation
55+
56+
## append
57+
58+
```golang=
59+
stockAmountByDay = append(stockAmountByDay, 170)
60+
```
61+
![image](https://hackmd.io/_uploads/S1pQsT8-xl.png)
62+
[append-example](https://go.dev/play/p/843TDDp3N6I)
63+
64+
## sub range reference
65+
```golang=
66+
stockAmountByDayRange := stockAmountByDay[1:4]
67+
```
68+
69+
![image](https://hackmd.io/_uploads/rJow3a8Wge.png)
70+
[sub-range-reference](https://go.dev/play/p/K9x8MOBA3gh)
71+
72+
73+
## copy
74+
```golang=
75+
stockAmountByRangeClone := make([]int, len(stockAmountByRange))
76+
copy(stockAmountByRangeClone, stockAmountByRange)
77+
```
78+
79+
![image](https://hackmd.io/_uploads/rkGH8C8Zge.png)
80+
81+
[copy-example](https://go.dev/play/p/4v2qE0jikrb)
82+
83+
84+
# 參考文件
85+
86+
[slice-intro](https://go.dev/blog/slices-intro)

arrays-and-slices/sum_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package arrays_and_slices
2+
3+
import "testing"
4+
5+
func TestSum(t *testing.T) {
6+
numbers := [5]int{1, 2, 3, 4, 5}
7+
8+
got := Sum(numbers)
9+
want := 15
10+
11+
if got != want {
12+
t.Errorf("got %d want %d ,given %v", got, want, numbers)
13+
}
14+
}

go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module github.com/leetcode-golang-classroom/learn-golang-with-tests
2+
3+
go 1.24.0

0 commit comments

Comments
 (0)