Posts

Json tag in golang

type Dummy struct { Name string `json:"name,omitempty"` Number int64 `json:"number,omitempty"` Pointer * string `json:"pointer,omitempty"` xxx string `json:"-"` // ignore this field yyy string `json:"-,"` // print this field with named - }

How to handle null data in SQL for struct in golang

type account struct { id int name string age sql . NullInt64 amt sql . NullFloat64 } func testReadNullvalue ( db * sqlx . DB ) { rows , _ := db . Query ( "select id, name, age , amount from account" ) for rows . Next () { var foo account rows . Scan (& foo . id , & foo . name , & foo . age , & foo . amt ) if foo . amt . Valid == true { fmt . Println ( "value: %v" , foo . amt . Float64 ) } else { fmt . Println ( "data is null, print with default value " , foo . amt . Float64 ) } fmt . Println ( "%+v" , foo ) } }

Postgresql

https://www.postgresql.org/docs/9.6/plpgsql-declarations.html drop table account; --create table CREATE TABLE account(    id SERIAL PRIMARY KEY,    name VARCHAR NOT null,    age numeric,    amount numeric ); -- insert data into table INSERT INTO account (name,age,amount) values ('bonh',27,0); INSERT INTO account (name,age,amount) values ('kynp',27,0); --- select * from account SELECT * FROM account; -- delete record by id delete from account where id >= 3; SELECT * FROM account; -- procedure for tranfering drop procedure if exists transfer; CREATE OR REPLACE PROCEDURE transfer(INT, INT, DEC) LANGUAGE plpgsql  AS $$ BEGIN     -- subtracting the amount from the sender's account     UPDATE account     SET amount = amount - $3     WHERE id = $1;     -- adding the amount to the receiver's account     UPDATE account     SET amount = amount ...

How to understand context in golang

Ref:  http://p.agnihotry.com/post/understanding_the_context_package_in_golang/

Go routine and how to use chan to get task' result with buffered chan P2

naturals := make ( chan int , 10 ) squares := make ( chan int ) go func () { for x := 0 ; ; x ++ { fmt . Println ( "Pushing" ) naturals <- x } }() go func () { for { fmt . Println ( "Getting out x" ) x := <- naturals squares <- x * x time . Sleep ( time . Second * 2 ) } }() for { fmt . Println (<- squares ) } naturals := make ( chan int , 10 ) As you can see that we use a buffered chan int with size 10 for naturals Then it can pushing to this chan maximum 10 element without waiting for getting out.

Go routine and how to use chan to get task' result

you should forget waitGroup :-) naturals := make ( chan int ) squares := make ( chan int ) go func () { for x := 0 ; ; x ++ { naturals <- x } }() go func () { for { x := <- naturals squares <- x * x } }() for { fmt . Println (<- squares ) } The above code is using unbuffered chan int. It means that that when naturals <-x then it must wait for getting x out by calling <-naturals then we can continue push naturals <-x. It is easy to say that it is not performance. In the next tutorial we will use buffered chan

Go routine and how to wait for the task done

it is simple wg := new ( sync . WaitGroup ) go func () { for i := 0 ; i < 10 ; i ++ { fmt . Println ( "Hello %d" , i ) time . Sleep ( time . Second / 2 ) } wg . Done () }() wg . Add ( 1 ) go func () { for i := 0 ; i < 10 ; i ++ { fmt . Println ( "Goodbye %d" , i ) time . Sleep ( time . Second ) } wg . Done () }() wg . Add ( 1 ) wg . Wait () Step 1: declare a wait group Step 2: at the end of task will will call wg.Done() to notify the task done Step 3: Specify exactly number of the task which need to wait for done by call wg.Add() If we have 3 task --> call wg.Add(3) or call 3 times wg.Add(1)