Skip to content

Commit 4fd6243

Browse files
author
xiao
committed
initial commit of 'starting state'
1 parent a05e688 commit 4fd6243

27 files changed

+575
-0
lines changed

module1/main.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package module1
2+
3+
import "fmt"
4+
5+
func main() {
6+
fmt.Println("hello world")
7+
}

module1/module1_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package module1
2+
3+
import (
4+
"os"
5+
"path"
6+
"testing"
7+
)
8+
9+
func TestModule1CheckEnv(t *testing.T) {
10+
actual := os.Getenv("GOPATH")
11+
expected0 := ""
12+
expected1 := path.Join(os.Getenv("HOME"), "go")
13+
14+
if actual != expected0 && actual != expected1 {
15+
t.Errorf("environment variable GOPATH not set properly")
16+
}
17+
}

module2/main.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package module2
2+
3+
import "fmt"
4+
5+
func main() {
6+
fmt.Println("hello world")
7+
}

module2/module2_code.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package module2
2+
import (
3+
"fmt"
4+
"runtime"
5+
)
6+
func content() {
7+
fmt.Println(runtime.GOOS)
8+
}

module2/module2_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package module2
2+
3+
import (
4+
"bufio"
5+
"log"
6+
"os"
7+
"testing"
8+
)
9+
10+
func TestModule2GoFormatContent(t *testing.T) {
11+
numOfLines := OpenFileAndCountLines("./module2_code.go")
12+
if numOfLines != 8 {
13+
t.Errorf("it looks your 'go fmt' does not work as we expected")
14+
}
15+
16+
// TODO: add more tests
17+
}
18+
19+
// OpenFileAndCountLines opens a file and returns number of lines in the file
20+
func OpenFileAndCountLines(filename string) int {
21+
f, err := os.Open(filename)
22+
if err != nil {
23+
log.Fatal(err)
24+
}
25+
defer f.Close()
26+
27+
scanner := bufio.NewScanner(f)
28+
number := 0
29+
for scanner.Scan() {
30+
number++
31+
}
32+
33+
return number
34+
}

module3/main.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package module3
2+
3+
import "fmt"
4+
5+
func main() {
6+
fmt.Println("hello world")
7+
}

module3/module3_test.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package module3
2+
3+
import (
4+
"bufio"
5+
"log"
6+
"os"
7+
"strings"
8+
"testing"
9+
)
10+
11+
func TestModule3GoFormatContent(t *testing.T) {
12+
expected := " github.com/nathan-osman/go-sunrise"
13+
found := OpenFileAndFindNthString("../go.mod", 0, expected)
14+
if found != true {
15+
t.Errorf("the go-sunrise package is not found")
16+
}
17+
}
18+
19+
// OpenFileAndFindNthString opens a file, look for Nth string splitted by a space, and return if given expected string is found or not
20+
func OpenFileAndFindNthString(filename string, nth int, expected string) bool {
21+
f, err := os.Open(filename)
22+
if err != nil {
23+
log.Fatal(err)
24+
}
25+
defer f.Close()
26+
27+
scanner := bufio.NewScanner(f)
28+
scanner.Split(bufio.ScanLines)
29+
30+
for scanner.Scan() {
31+
t := scanner.Text()
32+
trimmed := strings.Trim(t, " ")
33+
if trimmed == "" {
34+
continue
35+
}
36+
37+
// matching logic
38+
ss := strings.Split(trimmed, " ")
39+
if ss[nth] == expected {
40+
return true
41+
}
42+
}
43+
44+
return false
45+
}

module4/main.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package module4
2+
3+
import "fmt"
4+
5+
func main() {
6+
fmt.Println("hello world")
7+
}

module4/module4_test.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package module4
2+
3+
import (
4+
"bufio"
5+
"log"
6+
"os"
7+
"strings"
8+
"testing"
9+
)
10+
11+
func TestModule4GoFormatContent(t *testing.T) {
12+
expected := " github.com/codemodus/kace"
13+
found := OpenFileAndFindNthString("../go.mod", 0, expected)
14+
if found != true {
15+
t.Errorf("the kace package is not found")
16+
}
17+
}
18+
19+
// OpenFileAndFindNthString opens a file, look for Nth string splitted by a space, and return if given expected string is found or not
20+
func OpenFileAndFindNthString(filename string, nth int, expected string) bool {
21+
f, err := os.Open(filename)
22+
if err != nil {
23+
log.Fatal(err)
24+
}
25+
defer f.Close()
26+
27+
scanner := bufio.NewScanner(f)
28+
scanner.Split(bufio.ScanLines)
29+
30+
for scanner.Scan() {
31+
t := scanner.Text()
32+
trimmed := strings.Trim(t, " ")
33+
if trimmed == "" {
34+
continue
35+
}
36+
37+
// matching logic
38+
ss := strings.Split(trimmed, " ")
39+
if ss[nth] == expected {
40+
return true
41+
}
42+
}
43+
44+
return false
45+
}

module5/main.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package module5
2+
3+
import "fmt"
4+
5+
func main() {
6+
fmt.Println("hello world")
7+
}

module5/module5_code.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package module5
2+
3+
// GetExampleDotCom uses the "net/http" package to send a GET request to example.com
4+
func GetExampleDotCom() {
5+
resp, err := http.Get("http://example.com/")
6+
if err != nil {
7+
fmt.Println("something went wrong")
8+
}
9+
10+
defer resp.Body.Close()
11+
}

module5/module5_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package module5
2+
3+
import (
4+
"os"
5+
"path"
6+
"testing"
7+
)
8+
9+
func TestModule5InstallationOfgoimports(t *testing.T) {
10+
binPath := path.Join(os.Getenv("GOPATH"), "bin")
11+
found := FindFileAtPath(binPath, "goimports")
12+
13+
if !found {
14+
t.Errorf("goimports cannot be found")
15+
}
16+
}
17+
18+
// FindFileAtPath returns if theFilename is found at thePath
19+
func FindFileAtPath(thePath string, theFilename string) bool {
20+
if _, err := os.Stat(path.Join(thePath, theFilename)); os.IsNotExist(err) {
21+
return false
22+
}
23+
return true
24+
}

module6/main.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package module6
2+
3+
import "fmt"
4+
5+
func main() {
6+
fmt.Println("hello world")
7+
}

module6/module6.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package module6
2+
3+
// FunctionForModule6GoDoc is a function with some comment
4+
func FunctionForModule6GoDoc() {
5+
}

module6/module6.txt

Whitespace-only changes.

module6/module6_test.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package module6
2+
3+
import (
4+
"bufio"
5+
"log"
6+
"os"
7+
"strings"
8+
"testing"
9+
)
10+
11+
func TestModule6DummyTest(t *testing.T) {
12+
found := OpenFileAndFindString("module6.txt", "func FunctionForModule6GoDoc()")
13+
14+
if !found {
15+
t.Errorf("go doc does not work as expected")
16+
}
17+
18+
// TODO: add more tests
19+
}
20+
21+
// OpenFileAndFindString opens a file and return if the given string is found or not
22+
func OpenFileAndFindString(filename string, expected string) bool {
23+
f, err := os.Open(filename)
24+
if err != nil {
25+
log.Fatal(err)
26+
}
27+
defer f.Close()
28+
29+
scanner := bufio.NewScanner(f)
30+
scanner.Split(bufio.ScanLines)
31+
32+
for scanner.Scan() {
33+
t := scanner.Text()
34+
trimmed := strings.Trim(t, " ")
35+
if trimmed == "" {
36+
continue
37+
}
38+
39+
// matching logic
40+
if trimmed == expected {
41+
return true
42+
}
43+
}
44+
45+
return false
46+
}

module7/main.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package module7
2+
3+
import "fmt"
4+
5+
func main() {
6+
fmt.Println("hello world")
7+
}

module7/module7.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package module7
2+
3+
//go:generate goimports -w module7_code.go

module7/module7_code.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package module7
2+
3+
func content() {
4+
fmt.Println(runtime.GOOS)
5+
}

task/module0.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
## Purpose of this module
2+
This module will give you the guidance of installing `go` on MacOS and Windows.
3+
At the end of the module, you will have a working environment of `go`.
4+
5+
## Installation on MacOS
6+
(see video)
7+
8+
## Installation on Windows
9+
(see video)

task/module1.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
## Purpose of this module
2+
This module will provide information about retrieving the environment information in `go`.
3+
Then you can examining the `go` environment manually or through a tool like `grep`.
4+
5+
## Verify locally
6+
To test this module locally:
7+
* Open up a terminal at the project root
8+
* Run command `cd module1` to change to the `module1` directory
9+
* Run command `go test -run Module1` to run all tests for module 1, or
10+
* Run command `go test -v -run Module1` to run all tests for module 1 with verbose information
11+
12+
13+
## Task 1: Print the environment information
14+
First, refer to the [Print Go environment information](https://golang.org/cmd/go/#hdr-Print_Go_environment_information) section in the official documentation.
15+
16+
Then, type in the command for printing the environment information.
17+
In this task, we only need to specify the sub-command after `go`, and do not need to specify any flags after it.
18+
19+
## Task 2: Print the environment information and in JSON format
20+
By default, `go env` outputs as a shell script on Linux / MacOS and a batch script on Windows.
21+
22+
[JavaScript Object Notation (JSON)](https://en.wikipedia.org/wiki/JSON) is a common format for data exchanging.
23+
24+
Recall what you have read in the above documentation and check flags that are available to the `go env` command.
25+
What is the flag to specify the output to be in JSON format?
26+
27+
Write the complete command (go + sub-command + flag) under your answer for Task 1.
28+
29+
## Extra help
30+
Here are the commands to get more information on `go env` and `Go` environment:
31+
- `go help env`
32+
- `go help environment`

0 commit comments

Comments
 (0)