Posts

Difference between "docker stop" and "docker kill"

 To stop a running container, you can use either "docker stop" or "docker kill" command to do so. Although it seems doing the same thing, actually there is a slight difference between them. "docker stop" command will send a hardware signal SIGTERM to the container, if the process received the SIGTERM signal, the developer can code to do some clean up and releasing some resources before terminating the process. However "docker kill" command will send a hardware signal SIGKILL to the container and stop it immediately. Usually "docker stop" is used to stop a running container. If the container is hanging there for a long time or failed to stop. Then you can try to use "docker kill"to kill the container anyway.

Why do we need an accordant pace in computer systems

In the past, we always know clock is used to adjust the pace of the computer systems. But we don't understand why we need an accordant pace in computer systems. Why don't we just let the components in the computer do as much and fast as they can? The reason is that the different components have dependency between each other. So if we image the whole computer systems is a succession program, of course we don't need accordant pace any more. But this will make the components in computer wait for each other's result to go on. So this can't make components work in parallel way. So obviously clock has this advantage which is that it can make different components work in parallel way become possible.

DO NOT use the return of append function to create new slice in Go

For Golang developers, it is very common to use append function to append new elements into slice. The common syntax is like below: var slice := make([]int, 0) slice = append(slice, 123) And for some requirements, you may want to create a new slice based on an existing slice. We know the below the code will work perfectly. slice := []int{1,2,3} newSlice := make([]int, 0) newSlice = append(newSlice, append(slice, 4)...) Below is complete example code to show the result: package main import ( "fmt" ) func main() { // here create the origin slice with cap 10 to make sure that // the slice is big enough to hold more data      slice := make([]int, 0, 10)      slice = append(slice, []int{1, 2, 3}...)      newSlice := make([]int, 0)      newSlice = append(newSlice, append(slice, 4)...)      fmt.Println(slice)      fmt.Println(newSlice)      fmt.Printf("The...

How to add optional arguments in Go function without impacting the existing function calls

Unlike C# you can put "Optional" keyword to add optional arguments in function, there is no direct way to create optional parameters in Go functions. And there is no method overloading in Go either. Even so, if you really need to add optional arguments to an existing function, maybe because this function is in a common library which is called by different software modules and you don't want to update all the modules for this new argument change, there is still a way to implement optional arguments thanks to variadic arguments in Go functions. Variadic arguments allow developer to put any number of trailing arguments as the input of functions. For example: package main import ( "fmt" ) func main() { fmt.Printf("1 + 2 = %d\n", add(1, 2)) fmt.Printf("1 + 2 + 3 + 4 + 5 = %d\n", add(1, 2, 3, 4, 5)) } func add(args ...int) int { sum := 0 for i:=0; i < len(args); i++ { sum += args[i] } return sum } The output is: 1 + 2 = 3 1 ...

Pointer Receiver vs Value Receiver

Receivers are same as parameters in function in Go. They are also passed by value when functions are called. Because of this, it is preferred to use pointers as receivers instead of passing values directly. This is because usually we use receivers to implement OOP programming style. If passed by value, the properties of the objects will not be able to update. For example, we have an object called "person", it has age property. Initially when created, the person has initialised the age to 0, then we use method to change it to 18. See code below: package main import ( "fmt" ) type Person struct { Name string Age int } func main() { person := Person{Name : "Leo"} fmt.Println(person) person.SetAge(18) fmt.Println(person) } func (p *Person) SetAge(age int) { p.Age = age } The output of the result is: {Leo 0} {Leo 18} However if the receiver is passed by value like below: func (p Person) SetAge(age int) { p.Age = age } Then the age...

Variables and Data Types in Javascript

This is a study note about Javascript from  https://www.geeksforgeeks.org/introduction-to-javascript-course-learn-how-to-build-a-task-tracker-using-javascript/ Javascript is untyped language. Once a variable is created in javascript using 'var', you can assign any other type of data to this variable. var text = 'Hello World' text = 123 Above code is valid in Javascript. Variable text can be assigned a string value and then later change to a number. 1. Difference between var and let in Javascript Basically, 'var' is function scoped, however 'let' is block scoped. https://www.geeksforgeeks.org/difference-between-var-and-let-in-javascript/ Let's use use 'let' for most of the cases because 'var' is confusing . 2. Number type in Javascript The number type in Javascript contains both integer and floating point numbers. Besides these numbers, we also have 'special-numbers' in Javascript that are: ' infinity ', ' -Infin...

Mistakes That Should Not Happen Again

 Database: DO NOT use "key" keyword as a column name, because it is a reserved keyword in MySQL. Docker: Clean docker using below command from time to time to release more memory: docker system prune Then type u and press "enter".