diff --git a/hello.go b/hello/hello.go similarity index 100% rename from hello.go rename to hello/hello.go diff --git a/hello_test.go b/hello/hello_test.go similarity index 100% rename from hello_test.go rename to hello/hello_test.go diff --git a/num/num.go b/num/num.go new file mode 100644 index 0000000..c035ea3 --- /dev/null +++ b/num/num.go @@ -0,0 +1,13 @@ +package num + +func IsPrime(n int) bool { + if n <= 1 { + return false; + } + for i := 2; i < n; i++ { + if n % i == 0 { + return false; + } + } + return true; +} diff --git a/num/num_test.go b/num/num_test.go new file mode 100644 index 0000000..52db523 --- /dev/null +++ b/num/num_test.go @@ -0,0 +1,24 @@ +package num + +import "testing" + +type checklist struct { + n int; + ret bool; +} + +func TestNum(t *testing.T) { + list := []checklist{ + {2, true}, + {3, true}, + {4, false}, + {57, false}, + {97, true}, + } + + for _, item := range list { + if item.ret != IsPrime(item.n) { + t.Errorf("n: %v faild, want %v", item.n, item.ret) + } + } +}