-
Go - Function closures and web middleware
摘要:package main import "fmt" func adder() func(int) int { sum := 0 return func(x int) int { sum += x return sum } } func main() { pos, neg := adder(), ad
阅读全文
-
Go - Logging to the System Log Service
摘要:Problem: You want to log in to the system log instead of your logfiles. Solution: Use the log/syslog package to write to syslog. Syslog is a standard
阅读全文
-
Go - Using Log Levels
摘要:Examples of log levels from high to low are: • Fatal • Error• Warn • Info • Debug To set up log levels for your logs, you can add the level to each li
阅读全文
-
Go - Logging to File
摘要:Problem: You want to log events to a logfile instead of standard error. Solution: Use the SetOutput function to set the log to write to a file. You us
阅读全文
-
Go - Inspecting Errors
摘要:Problem: You want to check for specific errors or specific types of errors. Solution: Use the errors.Is and errors.As functions. The errors.Is functio
阅读全文
-
Go - Wrapping an Error with Other Errors
摘要:Problem: You want to provide additional information and context to an error you receive before returning it as another error. Solution: Wrap the error
阅读全文
-
Go - Creating Customized Errors
摘要:Problem: You want to create custom errors to communicate more information about the error encountered. Solution: Create a new string - based error or
阅读全文
-
Go - Using Multiple Versions of the Same Dependent Packages
摘要:Problem: You want to use multiple versions of the same dependent packages in your code. Solution: Use the replace directive in the go.mod file to rena
阅读全文
-
Go - Requiring Local Versions of Dependent Packages
摘要:Problem: You want to use local versions of the dependent packages. Solution: Set up Go to use a vendor directory by running go mod vendor. Local versi
阅读全文
-
Go - 这个for循环是怎么退出的
摘要:package main import ( "fmt" "sync" "time" ) var workers = 3 func processItem(input <-chan int, output chan<- int, wg *sync.WaitGroup) { for { fmt.Prin
阅读全文
-
Go - ERROR: fatal error: all goroutines are asleep - deadlock!
摘要:main.go: package main import "fmt" func main() { ch := make(chan int) ch <- 1 a := <-ch fmt.Println(a) } Got error: zzh@ZZHPC:/zdata/MyPrograms/Go/tes
阅读全文
-
Go - Using channels to receive interrupts in a program
摘要:Within the Kubernetes deployment environment, applications will actually be sent the SIGTERM signal first if it has been decided the pod holding the a
阅读全文
-
Microservice - What are microservices, and why are microservices?
摘要:The concept of microservices is simply breaking a single large potential service into many smaller services that work together, hence, the name. One v
阅读全文
-
Docker - docker compose
摘要:docker-compose.yaml: version: '3.3' services: nginx: image: nginx:latest ports: - 8080:80 With regards to the preceding docker-compose file, we have t
阅读全文
-
Docker - Install Docker Compose
摘要:Install using the repository Set up the repository. Find distro-specific instructions in: Ubuntu | CentOS | Debian | Raspberry Pi OS | Fedora | RHEL |
阅读全文
-
Docker - ERROR: failed to solve: golang:latest: error getting credentials - err: exec: "docker-credential-desktop": executable file not found in $PATH, out: ``
摘要:Dockerfile: FROM golang:latest WORKDIR /app ADD . . RUN go env -w GOPROXY=https://goproxy.io,direct RUN go get RUN go build -o app . CMD ["/app/app"]
阅读全文
-
Docker - Expose a port
摘要:In order to access the nginx from our workstation, we would need to expose the port 80 from within the nginx container to our workstation. Run the ngi
阅读全文
-
Go - Run an application using systemd
摘要:The systemd tool fits our simple case of requiring the application to start on server boot-up as well as ensuring that the application is restarted in
阅读全文
-
Go - Separate external calls from our main logic
摘要:Original implementation: type SingleItem struct { Field string `json:"field"` Hour int `json:"hour"` Minute int `json:"minute"` ItemCode string `json:
阅读全文
-
Docker - Install and Uninstall
摘要:Uninstall old versions Before you can install Docker Engine, you need to uninstall any conflicting packages. Distro maintainers provide unofficial dis
阅读全文
-
VSCode - Go: Generate Unit Tests for Function
摘要:Right click the function name. Select item 'Go: Generate Unit Tests for Function' in the pop-up menu: A test file named <file name>_test.go is generat
阅读全文
-
Common Protocols
摘要:Common protocols that handle exchanging data between client and server applications:• HTTP protocol: This is more of a text-based protocol and is cons
阅读全文
-
Go - Proberb: Never use global variables
摘要:Never use global variables
阅读全文
-
Go - Proverb: Accept interfaces, return structs
摘要:fake.go package fake import "fmt" type ExampleStruct struct { Item1 string Item2 string Item3 string } func (e ExampleStruct) LogLine() { fmt.Printf("
阅读全文
-
Go - Performance Comparison by Fibonacci Functions
摘要:fibonacci.go package algorithms // Dynamic Programming func Fibonacci1(n int) int { if n <= 0 { return 0 } if n <= 2 { return 1 } previous1 := 1 previ
阅读全文
-
Zgo - Sort Algorithms
摘要:sort.go package algorithms func MergeSort(items []int) []int { n := len(items) var combined []int switch { case n <= 1: combined = items case n == 2:
阅读全文
-
Linux - zip unzip
摘要:$ ls readme readme $ zip readme zip error: Nothing to do! (readme.zip) $ ls readme* readme $ zip readme.zip readme adding: readme (deflated 4%) $ ls r
阅读全文
-
Docker - Error: permission denied while trying to connect to the Docker daemon socket at unix:///var/run/docker.sock
摘要:Adding user to the docker group can fix this issue: zzh@ZZHPC:~$ sudo usermod -aG docker zzh zzh@ZZHPC:~$ newgrp docker zzh@ZZHPC:~$ docker run hello-
阅读全文
-
Docker - docker-compose.yml for PostgreSQL
-
Ubuntu - snap info / apt info
摘要:zzh@ZZHPC:~$ sudo snap info docker name: docker summary: Docker container runtime publisher: Canonical✓ store-url: https://snapcraft.io/docker contact
阅读全文
-
Go - test coverage
-
Go - benchstat
摘要:zzh@ZZHPC:/zdata/MyPrograms/Go/aaa/Ch06/06_02$ go test -run NONE -bench . -count=5 -benchmem | tee cols.txt goos: linux goarch: amd64 pkg: zzh/aaa/Ch0
阅读全文
-
Regular Expression
摘要:POSIX basic and extended[edit] In the POSIX standard, Basic Regular Syntax (BRE) requires that the metacharacters ( ) and { } be designated \(\) and \
阅读全文
-
Go - benchmark profile CPU and Memory
摘要:zzh@ZZHPC:/zdata/MyPrograms/Go/aaa$ go test -run NONE -bench . goos: linux goarch: amd64 pkg: zzh/aaa cpu: Intel(R) Core(TM) i5-9600K CPU @ 3.70GHz Be
阅读全文
|