├── .gitignore ├── go.mod ├── main.go ├── async └── async.go └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | .idea -------------------------------------------------------------------------------- /go.mod: -------------------------------------------------------------------------------- 1 | module main 2 | 3 | go 1.21.7 4 | -------------------------------------------------------------------------------- /main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "fmt" 5 | "main/async" 6 | "time" 7 | ) 8 | 9 | func DoneAsync() int { 10 | fmt.Println("Warming up ...") 11 | time.Sleep(3 * time.Second) 12 | fmt.Println("Done ...") 13 | return 1 14 | } 15 | 16 | func main() { 17 | fmt.Println("Let's start ...") 18 | future := async.Exec(DoneAsync) 19 | fmt.Println("Done is running ...") 20 | val := future.Await() 21 | fmt.Println(val) 22 | } 23 | -------------------------------------------------------------------------------- /async/async.go: -------------------------------------------------------------------------------- 1 | package async 2 | 3 | import "context" 4 | 5 | // Future interface has the method signature for await 6 | type Future[K any] interface { 7 | Await() K 8 | } 9 | type future[K any] struct { 10 | await func(ctx context.Context) K 11 | } 12 | 13 | func (f future[K]) Await() K { 14 | return f.await(context.Background()) 15 | } 16 | 17 | // Exec executes the async function 18 | func Exec[K any, F func() K](f F) Future[K] { 19 | var r interface{} 20 | ch := make(chan struct{}) 21 | go func() { 22 | defer close(ch) 23 | r = f() 24 | }() 25 | return future[K]{ 26 | await: func(ctx context.Context) K { 27 | select { 28 | case <-ch: 29 | return r.(K) 30 | case <-ctx.Done(): 31 | return r.(K) 32 | } 33 | }, 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ![logo](https://hackernoon.com/images/3Ur17PtJhkV5UkAAJFu6z8t0fKg1-eh2032xi.jpeg) 2 | 3 | ## A demo application with async/await in Golang 4 | async/await is an extremely popular feature of modern languages like C#, Python, Javascript. 5 | It simplifies the asynchronous method execution structure and, it reads like synchronous code. 6 | So much easier to follow for developers. 7 | 8 | ## Contribution 9 | Want to contribute? Great! 10 | 11 | To fix a bug or enhance an existing code, follow these steps: 12 | 13 | - Fork the repo 14 | - Create a new branch (`git checkout -b improve-feature`) 15 | - Make the appropriate changes in the files 16 | - Add changes to reflect the changes made 17 | - Commit your changes (`git commit -am 'Improve feature'`) 18 | - Push to the branch (`git push origin improve-feature`) 19 | - Create a Pull Request 20 | 21 | ## License 22 | MIT © [MD Ahad Hasan](https://github.com/joker666) --------------------------------------------------------------------------------