Posts

Showing posts from January, 2020

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)

Go routine and how to async

it is simple go func () { for i := 0 ; i < 10 ; i ++ { fmt . Println ( "Hello %d" , i ) time . Sleep ( time . Second / 2 ) } }() with keyword go

Selecting for Share and Update in PostgreSQL

Image
Ref:  http://shiroyasha.io/selecting-for-share-and-update-in-postgresql.html https://docs.oracle.com/cd/E17952_01/mysql-5.1-en/innodb-locking-reads.html A regular select statement does not give you enough protection if you want to query data and make a change in the database related to it. Other transactions can update or delete the data you just queried. PostgreSQL offers additional select statements that lock on read and provide an extra layer of safety. This article explores the  select for share  and  select for update  statements, locks that are created with these statements, and provide examples for using these two select statements. Safely Updating Data Sometimes, applications read data from the database, process the data, and save the result back in the database. This is a classic example where the  select for update  can provide additional safety. Let’s consider the following example: BEGIN ; SELECT * FROM purchases WHERE p...

Transaction Isolation Levels in PostgreSQL

Image
http://shiroyasha.io/selecting-for-share-and-update-in-postgresql.html http://shiroyasha.io/transaction-isolation-levels-in-postgresql.html Misunderstanding transaction isolation levels can lead to disgusting side effects in your application. Debugging such issues can be more than painful. The SQL standard defines four levels of transaction isolation. Each of these isolation levels defines what happens if two concurrent processes try to read data updated by other processes. This article explores how PostgreSQL isolates your transactions by default, and explains the alternative options you can take to ensure the correctness of your data. We will also explore the performance cost of the various isolation levels, and the typical use case for each of them. The Isolation of Concurrent Transactions Before we dig into the theoretical explanation, let’s observe the default behaviour of PostgreSQL transactions. We want to explore what happens if two concurrent process access th...