├── .github
└── workflows
│ └── go.yml
├── .idea
├── Quality-Code-With-Go.iml
├── modules.xml
├── vcs.xml
└── workspace.xml
├── Chapter1
├── Functional
│ └── main.go
├── Functions
│ └── main.go
├── Importing-Packages
│ └── main.go
├── Object-Oriented
│ └── class.py
├── Packages
│ ├── Standard-Packages
│ │ └── main.go
│ └── Third-Party Packages
│ │ ├── get.sh
│ │ └── main.go
├── Procedural
│ └── main.go
├── Run-Go-Code
│ └── main.go
├── Statically Typed
│ └── main.go
└── Variables
│ └── Different-Ways-To-Define-Variables
│ └── main.go
├── Chapter3
├── Dependencies
│ └── main.go
└── Var-Names
│ └── main.go
├── Chapter4
├── AzureSDK
│ └── main.go
├── Benchmark-Testing
│ ├── main.go
│ └── main_test.go
├── Edge-Case
│ ├── addition_test.go
│ └── main.go
└── unit-test-example
│ └── main_test.go
├── Chapter5
├── Dockerfile
├── cloudstatuscheck.go
└── cloudstatuscheck_test.go
├── Chapter6
├── Linting
│ ├── golangcilint.md
│ └── goversionchecker.go
└── Static-Code-Analysis
│ ├── .scannerwork
│ ├── .sonar_lock
│ ├── report-task.txt
│ └── sonar-go-to-slang-windows-amd64.exe
│ ├── create-docker-instance.md
│ └── goversionchecker.go
├── Chapter7
└── godoc.md
├── Chapter8
├── Create-ContainerGroup
│ ├── createcontainergroup.go
│ ├── createcontainergroup_benchmark_test.go
│ ├── createcontainergroup_integration_test.go
│ ├── createcontainergroup_test.go
│ ├── go.mod
│ └── go.sum
└── readme.md
├── LICENSE
└── README.md
/.github/workflows/go.yml:
--------------------------------------------------------------------------------
1 | name: Go
2 |
3 | on:
4 | push:
5 | branches: [ master ]
6 | pull_request:
7 | branches: [ master ]
8 |
9 | jobs:
10 |
11 | build:
12 | name: Build
13 | runs-on: ubuntu-latest
14 | env:
15 | working-directory: ./Chapter6/Static-Code-Analysis/
16 | steps:
17 |
18 | - name: Set up Go 1.x
19 | uses: actions/setup-go@v2
20 | with:
21 | go-version: ^1.15
22 |
23 | - name: Check out code into the Go module directory
24 | uses: actions/checkout@v2
25 |
26 | - name: Build
27 | run: go build -v ./Chapter6/Static-Code-Analysis/
28 |
29 | - name: Run golangci-lint
30 | uses: golangci/golangci-lint-action@v2
31 | with:
32 | version: v1.29
33 |
--------------------------------------------------------------------------------
/.idea/Quality-Code-With-Go.iml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
--------------------------------------------------------------------------------
/.idea/modules.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/.idea/vcs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/.idea/workspace.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
71 |
72 |
73 |
74 |
75 |
76 |
77 |
78 |
79 |
80 |
81 |
82 |
83 |
84 |
85 |
86 |
87 |
88 |
89 |
90 |
91 |
92 |
93 |
94 |
95 |
96 |
--------------------------------------------------------------------------------
/Chapter1/Functional/main.go:
--------------------------------------------------------------------------------
1 | package main
2 |
3 | import (
4 | "fmt"
5 | )
6 |
7 | func main(
8 | add(1 int)
9 | )
10 |
11 | func add (x int) func(y int) int {
12 | return func(y int) int {
13 | fmt.Println("Hurray, I'm doing functional programming!")
14 | return x + y
15 | }
16 | }
--------------------------------------------------------------------------------
/Chapter1/Functions/main.go:
--------------------------------------------------------------------------------
1 | package main
2 |
3 | func AzureAuth()
4 | {
5 |
6 | }
--------------------------------------------------------------------------------
/Chapter1/Importing-Packages/main.go:
--------------------------------------------------------------------------------
1 | // Method 1 - All packages in one import statement
2 |
3 | import (
4 | "fmt"
5 | "os"
6 | )
7 |
8 | // Method 2 - Line by line
9 | import "fmt"
10 | import "os"
11 |
12 | // Method 3 - Automation
13 | import (
14 | "fmt"
15 | )
16 |
17 | fmt.Println("Hello, readers!")
--------------------------------------------------------------------------------
/Chapter1/Object-Oriented/class.py:
--------------------------------------------------------------------------------
1 | class Cars:
2 | def __init__(self, Make, Model, Year):
3 | self.Make = Make
4 | self.Model = Model
5 | self.Year = Year
6 |
7 | myCar = Cars("Ford", "F150", "2020")
8 |
9 | print(myCar.Make)
--------------------------------------------------------------------------------
/Chapter1/Packages/Standard-Packages/main.go:
--------------------------------------------------------------------------------
1 | package main
2 |
3 | import (
4 | "fmt"
5 | )
6 |
7 | func main() {
8 | fmt.Println("Hello Readers!")
9 | }
--------------------------------------------------------------------------------
/Chapter1/Packages/Third-Party Packages/get.sh:
--------------------------------------------------------------------------------
1 | go get github.com/Azure/azure-sdk-for-go
--------------------------------------------------------------------------------
/Chapter1/Packages/Third-Party Packages/main.go:
--------------------------------------------------------------------------------
1 | package main
2 |
3 | import (
4 | "github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2020-06-01/compute"
5 | )
6 |
7 | func main() {
8 | compute.
9 | }
--------------------------------------------------------------------------------
/Chapter1/Procedural/main.go:
--------------------------------------------------------------------------------
1 | package main
2 |
3 | func main() {
4 | step1()
5 | step2()
6 | step3()
7 | }
8 |
9 | func step1() {
10 |
11 | }
12 |
13 | func step2() {
14 |
15 | }
16 |
17 | func step3() {
18 |
19 | }
--------------------------------------------------------------------------------
/Chapter1/Run-Go-Code/main.go:
--------------------------------------------------------------------------------
1 | package main
2 |
3 | import (
4 | "fmt"
5 | )
6 |
7 | func main() {
8 | fmt.Println("Hello, readers!")
9 | }
--------------------------------------------------------------------------------
/Chapter1/Statically Typed/main.go:
--------------------------------------------------------------------------------
1 | package main
2 |
3 | import (
4 | "fmt"
5 | )
6 |
7 | func test() {
8 | var carType string = "Ford"
9 | var owned int = 1
10 |
11 | fmt.Println(carType + owned)
12 | }
13 |
--------------------------------------------------------------------------------
/Chapter1/Variables/Different-Ways-To-Define-Variables/main.go:
--------------------------------------------------------------------------------
1 | // Declaring a single variable without a default value
2 |
3 | var carType string
4 |
5 | // Declaring a single variable with a default value
6 |
7 | var carType string = "Ford"
8 |
9 | // Multiple variable declaration - Must be the same type
10 |
11 | var carType, carYear string = "Ford", "2020"
12 |
13 | // Shorthand variable declaration
14 | carType := "Ford"
--------------------------------------------------------------------------------
/Chapter3/Dependencies/main.go:
--------------------------------------------------------------------------------
1 | // Pulling from master
2 | go get github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2020-06-01/compute
3 |
4 | // Pulling from a different branch, for example, the `dev` branch
5 | go get github.com/Azure/azure-sdk-for-go/services/compute/mgmt/2020-06-01/compute@dev
--------------------------------------------------------------------------------
/Chapter3/Var-Names/main.go:
--------------------------------------------------------------------------------
1 | package main
2 |
3 | import "fmt"
4 |
5 | func main() {
6 | v := "Mike"
7 | fmt.Printf("Hello, my name is %s", v)
8 |
9 | name := "Mike"
10 | fmt.Printf("Hello, my name is %s", name)
11 | }
12 |
--------------------------------------------------------------------------------
/Chapter4/AzureSDK/main.go:
--------------------------------------------------------------------------------
1 | package main
2 |
3 | import (
4 | "context"
5 | "fmt"
6 | "log"
7 | "os"
8 |
9 | "github.com/Azure/azure-sdk-for-go/services/billing/mgmt/2020-05-01/billing"
10 | "github.com/Azure/go-autorest/autorest"
11 | "github.com/Azure/go-autorest/autorest/azure/auth"
12 | )
13 |
14 | func main() {
15 | subID := os.Args[1]
16 | invoiceName := os.Args[2]
17 |
18 | getInvoiceDate(subID, invoiceName)
19 | }
20 |
21 | func azureAuth() autorest.Authorizer {
22 | auth, err := auth.NewAuthorizerFromCLI()
23 |
24 | if err != nil {
25 | log.Println("There was an error authenticating with the current Azure CLI profile")
26 | }
27 |
28 | return auth
29 | }
30 |
31 | func getInvoiceDate(subID string, invoiceName string) {
32 | if subID == "" {
33 | log.Println("Please add in a subscription ID")
34 | }
35 |
36 | if invoiceName == "" {
37 | log.Println("Please add in an invoice name")
38 | }
39 |
40 | invoiceClient := billing.NewInvoicesClient(subID, subID)
41 |
42 | if azureAuth() == nil {
43 | log.Panicln("No Azure CLI auth detected!")
44 | }
45 |
46 | invoiceClient.Authorizer = azureAuth()
47 |
48 | getByID, err := invoiceClient.GetByID(context.Background(), invoiceName)
49 |
50 | if err != nil {
51 | log.Println(err)
52 | } else {
53 | fmt.Println(*getByID.InvoiceDate)
54 | }
55 | }
56 |
--------------------------------------------------------------------------------
/Chapter4/Benchmark-Testing/main.go:
--------------------------------------------------------------------------------
1 | package main
2 |
3 | func addition(x int) int {
4 | return x
5 | }
6 |
7 | func main() {
8 | addition(4)
9 | }
10 |
--------------------------------------------------------------------------------
/Chapter4/Benchmark-Testing/main_test.go:
--------------------------------------------------------------------------------
1 | package main
2 |
3 | import (
4 | "log"
5 | "testing"
6 | )
7 |
8 | func BenchmarkAddition(b *testing.B) {
9 | if b == nil {
10 | log.Println("The testing package contains no values")
11 | }
12 |
13 | addition(4)
14 |
15 | }
16 |
--------------------------------------------------------------------------------
/Chapter4/Edge-Case/addition_test.go:
--------------------------------------------------------------------------------
1 | package main
2 |
3 | import (
4 | "log"
5 | "testing"
6 | )
7 |
8 | func TestAddition(t *testing.T) {
9 | if t == nil {
10 | log.Println("The testing package contains no values")
11 | }
12 |
13 | got := addition(4)
14 |
15 | if got != 4 {
16 | t.Error("Did not equal 4")
17 | }
18 | }
19 |
--------------------------------------------------------------------------------
/Chapter4/Edge-Case/main.go:
--------------------------------------------------------------------------------
1 | package main
2 |
3 | func addition(x int) int {
4 | return x
5 | }
6 |
7 | func main() {
8 | addition(4)
9 | }
10 |
--------------------------------------------------------------------------------
/Chapter4/unit-test-example/main_test.go:
--------------------------------------------------------------------------------
1 | package main
2 |
3 | import (
4 | "testing"
5 |
6 | "github.com/stretchr/testify/assert"
7 | )
8 |
9 | func TestAddition(t *testing.T) {
10 | x := 2
11 | y := 2
12 |
13 | assert.Equal(t, x, y, "x and y should be the same")
14 | }
15 |
--------------------------------------------------------------------------------
/Chapter5/Dockerfile:
--------------------------------------------------------------------------------
1 | FROM golang:latest
2 |
3 | RUN mkdir /build
4 | WORKDIR /build
5 |
6 | RUN export GO111MODULE=on
7 | RUN go get github.com/AdminTurnedDevOps/Quality-Code-With-Go/Chapter5
8 | RUN cd /build && git clone https://github.com/AdminTurnedDevOps/Quality-Code-With-Go.git
9 |
10 | RUN cd /build/Quality-Code-With-Go/Chapter5 && go build cloudstatuscheck.go
11 | CMD exec /bin/bash -c "trap : TERM INT; sleep infinity & wait"
12 |
13 | ENTRYPOINT [ "/build/Quality-Code-With-Go/Chapter5/cloudstatuscheck", "--azure" ]
--------------------------------------------------------------------------------
/Chapter5/cloudstatuscheck.go:
--------------------------------------------------------------------------------
1 | package main
2 |
3 | import (
4 | "flag"
5 | "io/ioutil"
6 | "log"
7 | "net/http"
8 | "os"
9 | "strings"
10 | )
11 |
12 | func main() {
13 | azurePtr := flag.Bool("azure", false, "Azure Status Check")
14 |
15 | flag.Parse()
16 |
17 | if *azurePtr {
18 | azureStatus()
19 | }
20 | }
21 |
22 | func azureStatus() string {
23 | response, err := http.Get("https://status.azure.com")
24 |
25 | if err != nil {
26 | log.Println(err)
27 | os.Exit(1)
28 | }
29 |
30 | body, err := ioutil.ReadAll(response.Body)
31 | if err != nil {
32 | log.Println(err)
33 | os.Exit(1)
34 | }
35 |
36 | defer response.Body.Close()
37 |
38 | output := string(body)
39 |
40 | if !strings.Contains(output, "data-label=\"Good\"") {
41 | log.Println("A service is down in Azure")
42 | log.Println("Please visit https://status.azure.com for more info")
43 | } else {
44 | log.Println("All Azure Services Are Operational")
45 | }
46 |
47 | return ""
48 | }
49 |
--------------------------------------------------------------------------------
/Chapter5/cloudstatuscheck_test.go:
--------------------------------------------------------------------------------
1 | package main
2 |
3 | import (
4 | "net/http"
5 | "testing"
6 |
7 | "github.com/stretchr/testify/assert"
8 | )
9 |
10 | // Handler for HTTP testing
11 | func handlerFunc(w http.ResponseWriter, r *http.Request) {}
12 |
13 | func TestAzureStatus(t *testing.T) {
14 | httpGet := "https://status.azure.com"
15 | if httpGet != "https://status.azure.com" {
16 | t.Error("Ensure to use the status.azure.com page")
17 | }
18 |
19 | azurePtr := "--azure"
20 | if azurePtr != "--azure" {
21 | t.Error("Ensure to use the `--azure` flag")
22 | }
23 |
24 | assert.HTTPStatusCode(t, handlerFunc, "GET", "https://status.azure.com", nil, 200)
25 | assert.HTTPSuccess(t, handlerFunc, "GET", "https://status.azure.com", nil)
26 | }
27 |
--------------------------------------------------------------------------------
/Chapter6/Linting/golangcilint.md:
--------------------------------------------------------------------------------
1 | ## Run golangci-lint
2 | `golangci-lint run`
3 |
4 | ## Enable all linters
5 | `golangci-lint run --enable-all`
6 |
7 | ## Disable everything but `staticcheck`
8 | `golangci-lint run --disable-all -E staticcheck`
--------------------------------------------------------------------------------
/Chapter6/Linting/goversionchecker.go:
--------------------------------------------------------------------------------
1 | package main
2 |
3 | import (
4 | "log"
5 | "os"
6 | "os/exec"
7 | )
8 |
9 | func main() {
10 | isVersion()
11 | }
12 |
13 | func isVersion() *exec.Cmd {
14 | version := exec.Command("go", "version")
15 | version.Stdout = os.Stdout
16 |
17 | err1 := version.Run()
18 |
19 | if err1 != nil {
20 | log.Fatal(err1)
21 | }
22 |
23 | return version
24 | }
25 |
--------------------------------------------------------------------------------
/Chapter6/Static-Code-Analysis/.scannerwork/.sonar_lock:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/AdminTurnedDevOps/Quality-Code-With-Go/c01cc8bb5881bbdacc9bf5ebc170c31538d3611e/Chapter6/Static-Code-Analysis/.scannerwork/.sonar_lock
--------------------------------------------------------------------------------
/Chapter6/Static-Code-Analysis/.scannerwork/report-task.txt:
--------------------------------------------------------------------------------
1 | projectKey=GoVersionTest
2 | serverUrl=http://localhost:9000
3 | serverVersion=8.5.1.38104
4 | dashboardUrl=http://localhost:9000/dashboard?id=GoVersionTest
5 | ceTaskId=AXWoPnxml8madg9K40j_
6 | ceTaskUrl=http://localhost:9000/api/ce/task?id=AXWoPnxml8madg9K40j_
7 |
--------------------------------------------------------------------------------
/Chapter6/Static-Code-Analysis/.scannerwork/sonar-go-to-slang-windows-amd64.exe:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/AdminTurnedDevOps/Quality-Code-With-Go/c01cc8bb5881bbdacc9bf5ebc170c31538d3611e/Chapter6/Static-Code-Analysis/.scannerwork/sonar-go-to-slang-windows-amd64.exe
--------------------------------------------------------------------------------
/Chapter6/Static-Code-Analysis/create-docker-instance.md:
--------------------------------------------------------------------------------
1 | 1. `docker run -d --name sonarqube -p 9000:9000 sonarqube`
2 | 2. Open up a web browser and go to `http://localhost:90000`
--------------------------------------------------------------------------------
/Chapter6/Static-Code-Analysis/goversionchecker.go:
--------------------------------------------------------------------------------
1 | package main
2 |
3 | import (
4 | "log"
5 | "os"
6 | "os/exec"
7 | )
8 |
9 | func main() {
10 | isVersion()
11 | }
12 |
13 | func isVersion() *exec.Cmd {
14 | version := exec.Command("go", "version")
15 | version.Stdout = os.Stdout
16 |
17 | err1 := version.Run()
18 |
19 | if err1 != nil {
20 | log.Fatal(err1)
21 | }
22 |
23 | return version
24 | }
25 |
--------------------------------------------------------------------------------
/Chapter7/godoc.md:
--------------------------------------------------------------------------------
1 | 1. Open up a code repository that has a package or code that you wish to generate documentation from
2 | 2. Create a package for the Go program and install it on your localhost
3 | 3. Move the binary package to either your $GOROOT or $GOPATH
4 | 4. Install godoc - `go get -v golang.org/x/tools/cmd/godoc`
5 | 5. To run Godoc on localhost - `godoc -http=:6060`
--------------------------------------------------------------------------------
/Chapter8/Create-ContainerGroup/createcontainergroup.go:
--------------------------------------------------------------------------------
1 | package main
2 |
3 | import (
4 | "context"
5 | "log"
6 | "os"
7 |
8 | "github.com/Azure/azure-sdk-for-go/profiles/latest/containerinstance/mgmt/containerinstance"
9 | "github.com/Azure/azure-sdk-for-go/sdk/to"
10 | "github.com/Azure/go-autorest/autorest"
11 | "github.com/Azure/go-autorest/autorest/azure/auth"
12 | )
13 |
14 | func main() {
15 |
16 | subscriptionID := os.Args[1]
17 | resourceGroupName := os.Args[2]
18 | name := os.Args[3]
19 | location := os.Args[3]
20 |
21 | createContainerGroup(subscriptionID, resourceGroupName, name, location)
22 |
23 | }
24 |
25 | func azureAuth() autorest.Authorizer {
26 | auth, err := auth.NewAuthorizerFromCLI()
27 |
28 | if err != nil {
29 | log.Println("There was an error authenticating with the current Azure CLI profile")
30 | }
31 |
32 | return auth
33 | }
34 |
35 | func createContainerGroup(subscriptionID, resourceGroupName, name, location string) containerinstance.ContainerGroupsCreateOrUpdateFuture {
36 | containerGroup := containerinstance.NewContainerGroupsClient(subscriptionID)
37 |
38 | containerGroup.Authorizer = azureAuth()
39 | create, err := containerGroup.CreateOrUpdate(context.Background(), resourceGroupName, name, containerinstance.ContainerGroup{
40 | Name: &name,
41 | Location: &location,
42 | ContainerGroupProperties: &containerinstance.ContainerGroupProperties{
43 | IPAddress: &containerinstance.IPAddress{
44 | Type: containerinstance.Public,
45 | Ports: &[]containerinstance.Port{
46 | {
47 | Port: to.Int32Ptr(8080),
48 | Protocol: containerinstance.TCP,
49 | },
50 | },
51 | },
52 | OsType: containerinstance.Linux,
53 | Containers: &[]containerinstance.Container{
54 | {
55 | Name: to.StringPtr("gowebapi"),
56 | ContainerProperties: &containerinstance.ContainerProperties{
57 | Ports: &[]containerinstance.ContainerPort{
58 | {
59 | Port: to.Int32Ptr(8080),
60 | },
61 | },
62 | Image: to.StringPtr("adminturneddevops/golangwebapi:latest"),
63 | Resources: &containerinstance.ResourceRequirements{
64 | Limits: &containerinstance.ResourceLimits{
65 | MemoryInGB: to.Float64Ptr(1),
66 | CPU: to.Float64Ptr(1),
67 | },
68 | Requests: &containerinstance.ResourceRequests{
69 | MemoryInGB: to.Float64Ptr(1),
70 | CPU: to.Float64Ptr(1),
71 | },
72 | },
73 | },
74 | },
75 | },
76 | },
77 | })
78 |
79 | if err != nil {
80 | log.Println(err)
81 | }
82 | return create
83 | }
84 |
--------------------------------------------------------------------------------
/Chapter8/Create-ContainerGroup/createcontainergroup_benchmark_test.go:
--------------------------------------------------------------------------------
1 | package main
2 |
3 | import (
4 | "context"
5 | "testing"
6 |
7 | "github.com/Azure/azure-sdk-for-go/profiles/latest/containerinstance/mgmt/containerinstance"
8 | "github.com/Azure/azure-sdk-for-go/sdk/to"
9 | "github.com/Azure/go-autorest/autorest/azure/auth"
10 | )
11 |
12 | var subscriptionID string = "220284d2-6a19-4781-87f8-5c564ec4fec9"
13 | var location string = "eastus"
14 | var resourceGroupName string = "dev2"
15 | var name string = "mjltestcontainergroup"
16 |
17 | func BenchmarkContainerGroup(b *testing.B) {
18 | auth, err := auth.NewAuthorizerFromCLI()
19 |
20 | containerGroup := containerinstance.NewContainerGroupsClient(subscriptionID)
21 |
22 | containerGroup.Authorizer = auth
23 | create, err := containerGroup.CreateOrUpdate(context.Background(), resourceGroupName, name, containerinstance.ContainerGroup{
24 | Name: &name,
25 | Location: &location, ContainerGroupProperties: &containerinstance.ContainerGroupProperties{
26 | IPAddress: &containerinstance.IPAddress{
27 | Type: containerinstance.Public,
28 | Ports: &[]containerinstance.Port{
29 | {
30 | Port: to.Int32Ptr(8080),
31 | Protocol: containerinstance.TCP,
32 | },
33 | },
34 | },
35 | OsType: containerinstance.Linux,
36 | Containers: &[]containerinstance.Container{
37 | {
38 | Name: to.StringPtr("gowebapi"),
39 | ContainerProperties: &containerinstance.ContainerProperties{
40 | Ports: &[]containerinstance.ContainerPort{
41 | {
42 | Port: to.Int32Ptr(8080),
43 | },
44 | },
45 | Image: to.StringPtr("adminturneddevops/golangwebapi:latest"),
46 | Resources: &containerinstance.ResourceRequirements{
47 | Limits: &containerinstance.ResourceLimits{
48 | MemoryInGB: to.Float64Ptr(1),
49 | CPU: to.Float64Ptr(1),
50 | },
51 | Requests: &containerinstance.ResourceRequests{
52 | MemoryInGB: to.Float64Ptr(1),
53 | CPU: to.Float64Ptr(1),
54 | },
55 | },
56 | },
57 | },
58 | },
59 | },
60 | })
61 |
62 | if err != nil {
63 | b.Error(err)
64 | } else {
65 | b.Log(create)
66 | }
67 | }
68 |
--------------------------------------------------------------------------------
/Chapter8/Create-ContainerGroup/createcontainergroup_integration_test.go:
--------------------------------------------------------------------------------
1 | // +build integration
2 |
3 | package main
4 |
5 | import (
6 | "fmt"
7 | "testing"
8 | )
9 |
10 | func TestIntegration(t *testing.T) {
11 | fmt.Println("Integration Test Passed")
12 | }
13 |
--------------------------------------------------------------------------------
/Chapter8/Create-ContainerGroup/createcontainergroup_test.go:
--------------------------------------------------------------------------------
1 | // +build unit
2 |
3 | package main
4 |
5 | import (
6 | "io"
7 | "log"
8 | "net/http"
9 | "net/http/httptest"
10 | "reflect"
11 | "testing"
12 |
13 | "github.com/Azure/go-autorest/autorest/azure/auth"
14 | "github.com/stretchr/testify/assert"
15 | )
16 |
17 | func TestAzureAuth(t *testing.T) {
18 | auth, err := auth.NewAuthorizerFromCLI()
19 |
20 | if err != nil {
21 | t.Error("There was an error authenticating with the current Azure CLI profile")
22 | } else {
23 | t.Log(auth)
24 | }
25 | }
26 |
27 | func TestPort(t *testing.T) {
28 | port := "8080"
29 | assert.Contains(t, port, "8080")
30 | }
31 |
32 | // Table Driven Test scenario
33 | type StringResult struct {
34 | given string
35 | expected string
36 | }
37 |
38 | func TestContainerImageString(t *testing.T) {
39 | image := "adminturneddevops/golangwebapi:latest"
40 |
41 | testString := reflect.TypeOf(image).Kind()
42 |
43 | if testString == reflect.String {
44 | log.Println("")
45 | } else {
46 | t.Error("Image is not in string form. Ensure the Docker image is written as a string and try again")
47 | }
48 |
49 | var aString = []StringResult{
50 | {
51 | "adminturneddevops/golangwebapi:latest", "adminturneddevops/golangwebapi:latest",
52 | },
53 | }
54 |
55 | for _, test := range aString {
56 | result, _ := test.given, test.expected
57 | if result != test.expected {
58 | t.Fatal("No string found")
59 | }
60 | }
61 | }
62 |
63 | func TestContainerPortInt(t *testing.T) {
64 | port := 8080
65 |
66 | testInt32 := reflect.TypeOf(port).Kind()
67 |
68 | if testInt32 == reflect.Int {
69 | log.Println("")
70 | } else {
71 | t.Error("Port is not of type Int. Please ensure the port is of type Int")
72 | }
73 | }
74 |
75 | func TestLocationString(t *testing.T) {
76 | location := "eastus"
77 |
78 | testStringLocation := reflect.TypeOf(location).Kind()
79 |
80 | if testStringLocation == reflect.String {
81 | log.Println("")
82 | } else {
83 | t.Error("Location is not of type string. Please ensure the location is of type string")
84 | }
85 | }
86 |
87 | func TestHttpAzureSDK(t *testing.T) {
88 | handle := func(w http.ResponseWriter, r *http.Request) {
89 | io.WriteString(w, "status successful")
90 | }
91 |
92 | req, err := http.NewRequest("get", "https://github.com/Azure/azure-sdk-for-go", nil)
93 | if err != nil {
94 | t.Fatal("HTTP GET request to the SDK is nil")
95 | } else {
96 | w := httptest.NewRecorder()
97 | handle(w, req)
98 | }
99 | }
100 |
--------------------------------------------------------------------------------
/Chapter8/Create-ContainerGroup/go.mod:
--------------------------------------------------------------------------------
1 | module containergroup
2 |
3 | go 1.15
4 |
5 | require (
6 | github.com/Azure/azure-sdk-for-go v48.1.0+incompatible
7 | github.com/Azure/azure-sdk-for-go/sdk/to v0.1.2
8 | github.com/Azure/go-autorest/autorest v0.11.11
9 | github.com/Azure/go-autorest/autorest/azure/auth v0.5.3
10 | github.com/Azure/go-autorest/autorest/to v0.4.0 // indirect
11 | github.com/Azure/go-autorest/autorest/validation v0.3.0 // indirect
12 | github.com/stretchr/testify v1.3.0
13 | )
14 |
--------------------------------------------------------------------------------
/Chapter8/Create-ContainerGroup/go.sum:
--------------------------------------------------------------------------------
1 | github.com/Azure/azure-sdk-for-go v0.2.0-beta h1:wYBqYNMWr0WL2lcEZi+dlK9n+N0wJ0Pjs4BKeOnDjfQ=
2 | github.com/Azure/azure-sdk-for-go v48.1.0+incompatible h1:WvGfkBG4/kPNL+CPn+AV52NJpPUfUhibEOwBqGYErsg=
3 | github.com/Azure/azure-sdk-for-go v48.1.0+incompatible/go.mod h1:9XXNKU+eRnpl9moKnB4QOLf1HestfXbmab5FXxiDBjc=
4 | github.com/Azure/azure-sdk-for-go v48.2.0+incompatible h1:+t2P1j1r5N6lYgPiiz7ZbEVZFkWjVe9WhHbMm0gg8hw=
5 | github.com/Azure/azure-sdk-for-go/sdk/to v0.1.2 h1:TZTVOb/ce7nCmOZYga9+ELtPPVVFG2Px4s/w5OycYS0=
6 | github.com/Azure/azure-sdk-for-go/sdk/to v0.1.2/go.mod h1:UL/d4lvWAzSJUuX+19uKdN0ktyjoOyQhgY+HWNgtIYI=
7 | github.com/Azure/go-autorest v1.1.1 h1:4G9tVCqooRY3vDTB2bA1Z01PlSALtnUbji0AfzthUSs=
8 | github.com/Azure/go-autorest v14.2.0+incompatible h1:V5VMDjClD3GiElqLWO7mz2MxNAK/vTfRHdAubSIPRgs=
9 | github.com/Azure/go-autorest v14.2.0+incompatible/go.mod h1:r+4oMnoxhatjLLJ6zxSWATqVooLgysK6ZNox3g/xq24=
10 | github.com/Azure/go-autorest/autorest v0.11.9/go.mod h1:eipySxLmqSyC5s5k1CLupqet0PSENBEDP93LQ9a8QYw=
11 | github.com/Azure/go-autorest/autorest v0.11.11 h1:k/wzH9pA3hrtFNsEhJ5SqPEs75W3bzS8VOYA/fJ0j1k=
12 | github.com/Azure/go-autorest/autorest v0.11.11/go.mod h1:eipySxLmqSyC5s5k1CLupqet0PSENBEDP93LQ9a8QYw=
13 | github.com/Azure/go-autorest/autorest/adal v0.9.5 h1:Y3bBUV4rTuxenJJs41HU3qmqsb+auo+a3Lz+PlJPpL0=
14 | github.com/Azure/go-autorest/autorest/adal v0.9.5/go.mod h1:B7KF7jKIeC9Mct5spmyCB/A8CG/sEz1vwIRGv/bbw7A=
15 | github.com/Azure/go-autorest/autorest/azure/auth v0.5.3 h1:lZifaPRAk1bqg5vGqreL6F8uLC5V0fDpY8nFvc3boFc=
16 | github.com/Azure/go-autorest/autorest/azure/auth v0.5.3/go.mod h1:4bJZhUhcq8LB20TruwHbAQsmUs2Xh+QR7utuJpLXX3A=
17 | github.com/Azure/go-autorest/autorest/azure/cli v0.4.2 h1:dMOmEJfkLKW/7JsokJqkyoYSgmR08hi9KrhjZb+JALY=
18 | github.com/Azure/go-autorest/autorest/azure/cli v0.4.2/go.mod h1:7qkJkT+j6b+hIpzMOwPChJhTqS8VbsqqgULzMNRugoM=
19 | github.com/Azure/go-autorest/autorest/date v0.3.0 h1:7gUk1U5M/CQbp9WoqinNzJar+8KY+LPI6wiWrP/myHw=
20 | github.com/Azure/go-autorest/autorest/date v0.3.0/go.mod h1:BI0uouVdmngYNUzGWeSYnokU+TrmwEsOqdt8Y6sso74=
21 | github.com/Azure/go-autorest/autorest/mocks v0.4.1/go.mod h1:LTp+uSrOhSkaKrUy935gNZuuIPPVsHlr9DSOxSayd+k=
22 | github.com/Azure/go-autorest/autorest/to v0.4.0 h1:oXVqrxakqqV1UZdSazDOPOLvOIz+XA683u8EctwboHk=
23 | github.com/Azure/go-autorest/autorest/to v0.4.0/go.mod h1:fE8iZBn7LQR7zH/9XU2NcPR4o9jEImooCeWJcYV/zLE=
24 | github.com/Azure/go-autorest/autorest/validation v0.3.0 h1:3I9AAI63HfcLtphd9g39ruUwRI+Ca+z/f36KHPFRUss=
25 | github.com/Azure/go-autorest/autorest/validation v0.3.0/go.mod h1:yhLgjC0Wda5DYXl6JAsWyUe4KVNffhoDhG0zVzUMo3E=
26 | github.com/Azure/go-autorest/logger v0.2.0 h1:e4RVHVZKC5p6UANLJHkM4OfR1UKZPj8Wt8Pcx+3oqrE=
27 | github.com/Azure/go-autorest/logger v0.2.0/go.mod h1:T9E3cAhj2VqvPOtCYAvby9aBXkZmbF5NWuPV8+WeEW8=
28 | github.com/Azure/go-autorest/tracing v0.6.0 h1:TYi4+3m5t6K48TGI9AUdb+IzbnSxvnvUMfuitfgcfuo=
29 | github.com/Azure/go-autorest/tracing v0.6.0/go.mod h1:+vhtPC754Xsa23ID7GlGsrdKBpUA79WCAKPPZVC2DeU=
30 | github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
31 | github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
32 | github.com/dimchansky/utfbom v1.1.0 h1:FcM3g+nofKgUteL8dm/UpdRXNC9KmADgTpLKsu0TRo4=
33 | github.com/dimchansky/utfbom v1.1.0/go.mod h1:rO41eb7gLfo8SF1jd9F8HplJm1Fewwi4mQvIirEdv+8=
34 | github.com/form3tech-oss/jwt-go v3.2.2+incompatible h1:TcekIExNqud5crz4xD2pavyTgWiPvpYe4Xau31I0PRk=
35 | github.com/form3tech-oss/jwt-go v3.2.2+incompatible/go.mod h1:pbq4aXjuKjdthFRnoDwaVPLA+WlJuPGy+QneDUgJi2k=
36 | github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y=
37 | github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
38 | github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
39 | github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
40 | github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
41 | github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
42 | github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
43 | golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
44 | golang.org/x/crypto v0.0.0-20201002170205-7f63de1d35b0 h1:hb9wdF1z5waM+dSIICn1l0DkLVDT3hqhhQsDNUmHPRE=
45 | golang.org/x/crypto v0.0.0-20201002170205-7f63de1d35b0/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
46 | golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
47 | golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
48 | golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
49 | golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
50 |
--------------------------------------------------------------------------------
/Chapter8/readme.md:
--------------------------------------------------------------------------------
1 | # Project Time
2 |
3 | The purposes of chapter 8, the final chapter, is to be fully project based. To take the best practices you've learned throughout chapters 1-7 and put them into a real-world example.
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2020 Mike Levan
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Quality-Code-With-Go
2 |
3 | Quality Code With Go is an eBook available on Gumroad that takes a different approach of learning development. Instead of a standard approach of learning the ins and outs of a language, Quality Code With Go goes through learning the ins and outs with a test-driven approach.
4 |
5 | Quality Code With Go covers how to properly test, document, use, and showcase Go code using the best practices available in 2020.
6 |
7 | ## How To Use This Repository
8 |
9 | Each code example is split into chapters. As you go through the book, the best approach is to open the directory for the chapter you're on and the code examples will be there.
10 |
11 | ## Usage
12 |
13 | The code examples can be used on all operating systems that have Go installed.
14 |
15 | ## Where Can You Find The Book
16 |
17 | If you're interested in Quality Code With Go, you can find it on [Gumroad](https://gumroad.com/l/ASyXy)
18 |
--------------------------------------------------------------------------------