Have Goroutine and C# async/await same effect


Using async/await aproach if process continue on different cpu from our application server is can be very useful.

For example a database query runs on database server, no need to block application thread while query is running. You can check it from here.

Lets look at that c# code block

 public async Task SaveAsync(User user)
        using (DataContext ctx = new DataContext())
            // database does its job while we are waiting
            await ctx.SaveChangesAsync();

So has goroutine code below same effect ?

func (u *User) Create(userModel *model.User) error {
var err error
var wg sync.WaitGroup
//or channel can be used for sync 
wg.Add(1)  //wg := make(chan int)

go func() {
    defer wg.Done(). //wg <- 1
    _, err = u.Transaction.User.

wg.Wait() //    <-wg
if err !=nil{
    return err
return nil}


Goroutines and async/await are different approaches to asynchronous programming. Under the hood you have P processes M CPU cores and G coroutines. Your language runtime operates them – checks when one should be stopped (unscheduled from real processes and thus CPU cores) and others should be scheduled to cores.

What approach is better? It depends on developer. As for me async/await has more syntax sugar, goroutines is more explicit in terms of programming. So first can be more concise, second – easier understandable in complex cases like select {..} and so on.

Answered By – Eugene Lisitsky

Answer Checked By – Marilyn (GoLangFix Volunteer)

Leave a Reply

Your email address will not be published.