Posts

Thread vs Process

  https://www.guru99.com/difference-between-process-and-thread.html Properties of Process Here are the important properties of the process: Creation of each process requires separate system calls for each process. It is an isolated execution entity and does not share data and information. Processes use the IPC(Inter-Process Communication) mechanism for communication that significantly increases the number of system calls. Process management takes more system calls. A process has its stack, heap memory with memory, and data map. Properties of Thread Here are important properties of Thread: Single system call can create more than one thread Threads share data and information. Threads shares instruction, global, and heap regions. However, it has its register and stack. Thread management consumes very few, or no system calls because of communication between threads that can be achieved using shared memory.

Why is TCP more reliable than UDP

https://www.ques10.com/p/11133/how-is-tcp-better-than-udp-explain-services-offe-1/  TCP is better than UDP because of following reasons: TCP is Reliable as it provides reliability of delivery of packets to the receiver while UDP is Non-reliable and does not give information about the packets. TCP is Connection oriented it means connection is to be setup before data is sent that is done in form of 3-way handshake while UDP is Connectionless. TCP provides flow control and error control characteristics while UDP doesn't provide it. TCP gives guarantee that a packet will reach on the destination without any duplication and the order of data will also be same.

What is the difference between 2 code blocks

package main import ( "fmt" "github.com/sirupsen/logrus" "runtime/debug" "time" ) func main () { defer func () { if err := recover (); err != nil { logrus . Errorln ( "main" , err ) debug . PrintStack () } }() GoInside ( nil ) time . Sleep ( 2 * time . Second ) fmt . Println ( "After panic" ) } type Test struct { X int } func GoInside ( v * Test ) { go NullPointer ( v . X ) } func NullPointer ( X int ) { fmt . Println ( X ) } This above code can handle a panic error , but the code below can not handle a panic ==> why  package main import ( "fmt" "github.com/sirupsen/logrus" "runtime/debug" "time" ) func main () { defer func () { if err := recover (); err != nil { logrus . Errorln ( "main" , err ) debug . PrintStack () } }() GoInside () time . Sleep ( 2 * ti...

How to handle panic error in Golang

ref:  https://ednsquare.com/story/learn-go-panic-and-recover-in-golang------1HSVNg func main () { NullPointer ( nil ) time . Sleep ( 2 * time . Second ) fmt . Println ( "After panic" ) } type Test struct { X int } func NullPointer ( v * Test ) { fmt . Println ( v . X ) } Đoạn code trên dễ dàng gây ra lỗi panic và stop hàm main panic: runtime error: invalid memory address or nil pointer dereference Vậy chúng ta cần làm gì để tránh gây chết hàm main đây Nếu là java hoặc ngôn ngữ khác thì chúng ta đã có try catch finally  Còn trong golang bạn sẽ có một cách trông đẹp mắt hơn func main () { defer func () { if err := recover (); err != nil { logrus . Errorln ( err ) debug . PrintStack () } }() NullPointer ( nil ) time . Sleep ( 2 * time . Second ) fmt . Println ( "After panic" ) } type Test struct { X int } func NullPointer ( v * Test ) { fmt . Println ( v . X ) } Theo cách này thì trong hàm main chúng ta ...

How to use library for stack and queue in golang

package main import ( "container/list" "fmt" ) func main () { stack := list . New () //[] stack . PushBack ( 2 ) //[2] stack . PushBack ( 3 ) //[3,2] stack . PushBack ( 4 ) //[4,3,2] stack . PushBack ( 5 ) //[5,4,3,2] stack . PushFront ( 6 ) //[5,4,3,2,6] e := stack . Front () //-> 6 fmt . Printf ( " %v " , e . Value ) e = stack . Front () fmt . Printf ( " %v " , e . Value ) v := stack . Remove ( stack . Front ()) //remove 6 fmt . Printf ( " %v " , v ) v = stack . Remove ( stack . Front ()) //remove 2 fmt . Printf ( " %v " , v ) v = stack . Remove ( stack . Back ()) //remove 5 fmt . Printf ( " %v " , v ) stack . Len () }

Hiểu về byte và rune trong Golang

tại sao Golang lại có 2 kiểu dữ liệu byte và rune // byte is an alias for uint8 and is equivalent to uint8 in all ways. It is // used, by convention, to distinguish byte values from 8-bit unsigned // integer values. type byte = uint8 // rune is an alias for int32 and is equivalent to int32 in all ways. It is // used, by convention, to distinguish character values from integer values. type rune = int32 Thực ra byte và rune là 2 kiểu alias của uint8 và int32 byte nó đại diện cho các ký tự không dấu rune nó đại diện cho các ký tự unicode nên nó có khoảng rộng hơn byte Ví dụ bạn có 2 chuỗi string như sau s1 := "a123acb" s2 := "a123acbaHôm nay quá đẹp" arr1 := [] byte ( s1 ) fmt . Printf ( " %v \n " , arr1 ) arr2 := [] rune ( s2 ) fmt . Printf ( " %v " , arr2 ) Như vậy khi bạn ép kiểu thì thao tác sẽ không bị mất dữ liệu

Format types in golang

https://gobyexample.com/string-formatting %s  dùng để format cho String:  fmt . Printf ( "%s\n" , "\"string\"" ) %c dùng để format 1 số nguyên về kiểu ký tự fmt . Printf ( "%c\n" , 33 ) %v dùng để format một struct, hoặc số, hoặc boolean, hoặc con trỏ về đúng kiểu giá trị của nó %T dùng để xác đinh type của kiểu dữ liệu fmt . Printf ( " %T\n " , p ) fmt . Printf ( " %t\n " , true ) %d dùng để in giá trị số nguyên %f dùng để in số thực fmt . Printf ( " %.2f\n " , 78.123123 )