Database:
DO NOT use "key" keyword as a column name, because it is a reserved keyword in MySQL.
Docker:
Then type u and press "enter".
DO NOT use "key" keyword as a column name, because it is a reserved keyword in MySQL.
Then type u and press "enter".
This is a study note for YouTube tutorial: https://www.youtube.com/watch?v=YS4e4q9oBaU&t=20036s
You can only create channels using make function.
Below is an example how to use channel to transfer data between goroutines.
The result is:
42
Be noted that channel operation will block the goroutine when executing. If you have unequal receiver and sender, then the additional goroutines will be blocked and give runtime errors.
27 fatal error: all goroutines are asleep - deadlock!
You can use bidirectional communication using channel in goroutines.
42 27
Goroutines can automatically manage the directions of the communication. However in most cases, we would like to make channel in single directions, which means one goroutine is dedicated to be receiver and another be a sender. To do this, you can put channel as function parameter for goroutine to restrict the data flow. Because function parameter can be used to cast a bi channel to a single direction channel.
42
Now the first goroutine is restricted to receive-only, and the second is restricted to send-only.
You can add a buffer for a channel by simply specifying the second parameter of make function with buffer size.
27 will be buffered instead of throwing a "deadlock" error.
The above code is not a correct way to handle buffered channels. "for...range" loops is preferred in this case.
The result is:
42 27 fatal error: all goroutines are asleep - deadlock!
You can see there is "deadlock" error again, but this time it is not because of the sender goroutine, it is because of the for range loop in the receiver goroutine. Because the receiver goroutine keeps trying to receive data even there is no data sent anymore.
To solve this problem, you can use "close(ch)" function to close channel in the sender goroutine if there is no data to send anymore
The result is:
42 27
In fact, channel has a second return value which you can use for checking if the channel is open or closed.
The result is:
42 27
You can use a select statement to manage the workflow in a goroutine. Select statement is similar to switch statement, but select statement is only used for communications. The case statement refers to the communications on different channels.
References: https://www.youtube.com/watch?v=YS4e4q9oBaU&t=20036s
This is a study note for YouTube tutorial: https://www.youtube.com/watch?v=YS4e4q9oBaU&t=20036s
Goroutines are green threads. Green threads are created and managed by applications instead of kernel like the native threads. One of the benefits in green thread is that it can be created very fast.
To illustrate further, below is slightly more complicated example that uses two goroutine.
The result is:
Hello #3
Hello #1
Hello #7
Hello #8
Hello #7
Hello #0
Hello #7
Hello #3
Hello #0
Hello #10
From above result, you see the number is not in sequence. In order to do that, you need to use mutex to lock the shared variables between goroutines.
Actually this example has no sense, because it is totally remove the multi threading advantages.
You can use "runtime.GOMAXPROCS(int)" function to set the number of low-level threads you want to use. If you set the parameter to be -1, the function return the thread number you set or return the number of cores by default if you didn't set any.
Threads: 8 Threads: 1
The number is better to use the number of cores, because if you set a big value, you could slow down the performance.
References: https://www.youtube.com/watch?v=YS4e4q9oBaU&t=20036s
This is a study note from YouTube tutorial: https://www.youtube.com/watch?v=YS4e4q9oBaU&t=12098s
Interface is a collection of function prototypes. You can define interface like below:
Interfaces should be implemented as methods.
You can use interface to realise polymorphism like below:
You can embed interface into another interface like structs. Below is a writer example code from the tutorial video.
Interface variable can be converted to the value type (structs or other types) using dot oeprtation.
You can check the if the conversion is successful or failed by checking the second return value from the conversion operation.
The result is:
Conversion failed
References: https://www.youtube.com/watch?v=YS4e4q9oBaU&t=12098s
The startup chime is the only reason I want to smash my Macbook Pro sometimes. And Apple didn't provide an easy way to turn this thing off. I could not find the setting below in Apple support although it mentioned that it should be there for MacOS Big Sur 11 or later. However I am using the latest MacOS Catalina 10, but I still cannot find it.
But I found a solution that works for me.
You can try the below command to turn it off.
sudo nvram StartupMute=%01
If you want to turn it on.
sudo nvram StartupMute=%00
For me, I prefer to keeping it mute forever.
This is a study note for YouTube tutorial: https://www.youtube.com/watch?v=YS4e4q9oBaU&t=12079s
This post only talks about some special parts of Go functions.
Like variables, functions starting with lower case will keep private to the package, however functions starting with upper case will be exposed to the outside of the package.
The output is:
Hello Tiffany
2, you can use variadic parameters in Go.
The output is:
The sum is 15
You can use variadic parameter with other parameter, but the function can only have one variadic parameter and it must be at the end.
2, You can return a pointer from a function as well. Unlike in C/C++, when you turn a pointer in call stack, you can get as undefined behaviour because the stack will be destroyed when the function returns. Thus, you cannot control what is in the pointer now. However in Go, when Go detect you are returning a pointer from stack, it will automatically promote this variable from stack to heap, so you don't need to be worry about the value in the pointer is destroyed.
3, You can use multiple returns in Go functions. The most useful cases of multiple returns is to add an error message in a function to indicate if the function execution is successful.
You can specify the signature of the function like below.
Reference: https://www.youtube.com/watch?v=YS4e4q9oBaU&t=12079s
This is a study note for YouTube: https://www.youtube.com/watch?v=YS4e4q9oBaU&t=12079s
Except conditional statement and looping, you can also use defer, panic and recover to manage control flow in Go.
Deferred functions are executed in LIFO (Last In First Out) order. For exampe:
The result is:
End! Middle! Start!
Defer function is very useful, one example is that you can use refer function to do some connection closing, resource clean up tasks. For example, in below code, you can put a close function right after you establish the connection before you even start to use the connection. This is because the close function is deferred, it will only execute after the calling function exits.
Another important thing to mention is that the deferred functions only take the variable value from the time is deferred, the change after the deferred function call will not impact the input value in the deferred function. For example:
The result is:
start
You can see that the value is still "start" even though variable "a" was changed to "end" later.
Panic is kinda like Exception in other programming language like JAVA. When panic function is executed, it will log the the position where causes the panic and then exit.
The result is:
(base)MyLaptop$ go run src/github.com/mingdos/firstapp/Main.go
start
panic: something bad happened
goroutine 1 [running]:
main.main()
/Users/caimingda/go/src/github.com/mingdos/firstapp/Main.go:10 +0x95
exit status 2
When works with deferred function, The execution order is like below:
Normal execution -> (defer function calling) -> normal execution -> (panic happens) -> defer function execution -> panic function execution
The result is:
(base)MyLaptop$ go run src/github.com/mingdos/firstapp/Main.go
start
this was deferred
panic: something bad happened
goroutine 1 [running]:
main.main()
/Users/caimingda/go/src/github.com/mingdos/firstapp/Main.go:11 +0x95
exit status 2
The result is:
start Error: something bad happened
The recover() function does more than this actually. It will recover the execution from where after the caller function who cause the panic inside and continue to execute the rest.
The result is:
start about to panic Error: something bad happened end
If the defer function don't know how to handle the panic, it actually can redo a panic when it detect a panic. In this way, the caller function will still have the panic.
Reference: https://www.youtube.com/watch?v=YS4e4q9oBaU&t=12079s
This is a study note of YouTube tutorial: https://www.youtube.com/watch?v=YS4e4q9oBaU&t=7285s
Here I will only discuss the special usage of conditional statements in Go compared to other traditional programming languages like Java, C/C++...
Here I give an example directly from the tutorial.
The result is:
20612439
There are two parts in above if statement if pop, ok := statePopulations["Florida"]; ok. The first part before the semicolon is the initialiser where you can initialise some variables. The second part after the semicolon is the condition. Be aware that the variables defined in the initialiser part are only alive in the if block. If you try to use the variable ("pop" and "ok") after the if statement, you will get an undefined variable error.
The result is:
two
You can also put multiple conditions in one case like below:
The result is:
one, five or ten
You can also use initialise in switch statement:
The result is:
one, five or ten
Reference: https://www.youtube.com/watch?v=YS4e4q9oBaU&t=7285s
This is a study note from YouTube tutorial: https://www.youtube.com/watch?v=YS4e4q9oBaU&t=7285s
You can define a struct and initialise it using following example:
You can dot operator (".") to access the fields in a struct.
You can create an anonymous struct for informal uses in Go.
Unlike map is a reference type, struct is a value type. This means if you assign one struct to another, the struct will be made a copy and assign to the other. Any modification in one struct won't impact the others.
The result is:
{John Pertwee} {Tom Baker}
Same like array, if you want two struct refer to the same location, you can use pointer to do so.
The result is:
{Tom Baker} &{Tom Baker}
You can embed a struct in another struct by simply declaring the embedding struct in the other. For example:
The result is:
{{Emu Australia} 48 false}
You can also initialise the struct in a structured way, however you can still refer the fields in the embedded struct directly.
The result is:
Emu
Reference: https://www.youtube.com/watch?v=YS4e4q9oBaU&t=7285s
This is a study note from YouTube tutorial: https://www.youtube.com/watch?v=YS4e4q9oBaU&t=7285s
You can also use make function to create map:
Like dictionary in C#, you can simply access a map value using key - statePopulations["Ohio"].
You can also add key-value pair in map like dictionary in c#:
You can use delete built-in function to delete a key in map:
The first parameter is the map, and second parameter is the key you want to delete.
Will this give an error or exception? The answer is neither.
The result is:
0
From the example above, you can see the value is 0 when key does not exist. This will cause problem if you use the value directly because you don't know "0" means key does's exist or the population is 0. However there is a solution: you can use comma separated expression to get another boolean value from map.
The result is:
0 false
If you don't care about the value for now, you can simple use the write-only operator "_" to skip the value and get the true/false result only.
Map is also a reference type, so if you have two map variable refer to the same memory, when one is modified, the other is modified as well.
In above example, you can see the result that, "Ohio" is removed from both "statePopulations" and "a" referencces.
Reference: https://www.youtube.com/watch?v=YS4e4q9oBaU&t=7285s
(This story is written by me and I will be posting it on Royal Road. Profile: https://www.royalroad.com/profile/1129550/fictions) Six fifty ...