Posts

Showing posts from April, 2021

Channels in Go

 This is a study note for YouTube tutorial:  https://www.youtube.com/watch?v=YS4e4q9oBaU&t=20036s Channel basics You can only create channels using make function. ch := make ( chan int ) // chan int means create a integer channel Below is an example how to use channel to transfer data between goroutines. var wg = sync.WaitGroup{} func main () { ch := make ( chan int ) wg. Add ( 2 ) go func () { i := <-ch fmt. Println (i) wg. Done () }() go func () { ch <- 42 wg. Done () }() wg. Wait () } The result is: 42 Restricting data flow 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. func main () { ch := make ( chan int ) wg. Add ( 3 ) go func () { i := <-ch fmt. Println (i) wg. Done () }() go func () { ...

Goroutines

 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. Creating goroutines Use keyword "go" before the invocation of the function to turn normal functions into goroutines. func main () { go sayHello () } func sayHello () { fmt. Println ( "Hello" ) } In many cases, we may need anonymous functions to run goroutines. func main () { var msg = "Hello" go func () { fmt. Println (msg) }() time. Sleep ( 100 * time.Millisecond) } The result is: Hello From above example, you can see that goroutines are able to use the variable in the outer space. This is handled by go runtime. However this is not recommended to use, because later when you change the variable, it could impact the behavi...

Interfaces in Go

 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: type Writer interface { Write ([] byte ) ( int , error ) } Interfaces should be implemented as methods. type ConsoleWriter struct {} func (cw ConsoleWriter) Write (data [] byte ) ( int , error ) { n , err := fmt. Println ( string (data)) return n, err } Polymorphism You can use interface to realise polymorphism like below: func main () { var w Writer = ConsoleWriter{} // Here can use different structs //with different implementation w. Write ([] byte ( "Hello go!" )) } Implicit implementation Except polymorphism, you can use interface to do implicit implementation. What does implicit implementation means? Since you don't need to use "implements" keyword to implement an interface like JAVA and some other language...

Turn off the startup chime permanently using terminal

Image
 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.

Special handling of functions in Go

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. Function name 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. Function parameters 1, if the function has parameters with same type, you can simply put the type at the last parameter in the function. For example. func main () { greet ( "Hello" , "Tiffany" ) } func greet (greeting, name string ) { println (greeting, name) } The output is: Hello Tiffany 2, you can use variadic parameters in Go. func main () { sum ( 1 , 2 , 3 , 4 , 5 ) } func sum (values ... int ) { sum := 0 for _ , v := range values { sum += v } fmt. Println ( "The sum is " , sum) } The output is: The sum is 15 You can use variadic parameter with other parameter,...

Defer, panic and recover in Go

 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. Defer You can use "defer" keyword to defer the execution of a function when the calling function exits but before it returns . For example: func main () { fmt. Println ( "Start!" ) defer fmt. Println ( "Middle!" ) fmt. Println ( "End!" ) } The result is: Start! End! Middle! Deferred functions are executed in LIFO (Last In First Out) order. For exampe: func main () { defer fmt. Println ( "Start!" ) defer fmt. Println ( "Middle!" ) defer fmt. Println ( "End!" ) } 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 r...

Special handing of conditional statements in Go

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++... Initialiser syntax Here I give an example directly from the tutorial. func main () { statePopulations := map [ string ] int { "California" : 39250017 , "Texas" : 27862596 , "Florida" : 20612439 , "Pennsylvania" : 12802503 , "Illinois" : 12801539 , "Ohio" : 11614273 , } if pop , ok := statePopulations[ "Florida" ]; ok { fmt. Println (pop) } } 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 p...

Structs in Go

 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: type Doctor struct { Number int ActorName string Companions [] string } func main () { aDoctor := Doctor{ Number: 3 , ActorName: "Jon Pertwee" , Companions: [] string { "Liz Shaw" , "Jo Grant" , "Sarah Jane Smith" , }, } fmt. Println (aDoctor) fmt. Println (aDoctor.ActorName) } You can dot operator (".") to access the fields in a struct. Anonymous struct  You can create an anonymous struct for informal uses in Go. func main () { aDoctor := struct { name string }{name: "John Pertwee" } fmt. Println (aDoctor) } Struct is a value type Unlike map is a reference type, struct is a value type. This means if you assign one struct to another, the struc...

Maps in Go

 This is a study note from YouTube tutorial:  https://www.youtube.com/watch?v=YS4e4q9oBaU&t=7285s Maps can be created in Go using type  map [ string ] int {} . func main () { statePopulations := map [ string ] int { "California" : 39250017 , "Texas" : 27862596 , "Florida" : 20612439 , "New York" : 19745289 , "Pennsylvania" : 12802503 , "Illinois" : 12801539 , "Ohio" : 11614373 , } fmt. Println (statePopulations) } You can also use make function to create map: func main () { statePopulations := make ( map [ string ] int ) // you can use make(map[string]int, 100)                                                         //  to specify length as well statePopulations = map [ string ] int { ...