Skip to content

Instantly share code, notes, and snippets.

View phuctm97's full-sized avatar
πŸ‘¨β€πŸ’»
Coding

Minh-Phuc Tran phuctm97

πŸ‘¨β€πŸ’»
Coding
View GitHub Profile
@phuctm97
phuctm97 / entities.go
Created September 27, 2019 16:27
Go RESTful Series
// Errors.
var (
ErrUsernameIsTooShort = errors.New("username must be at least 1-character length")
ErrUsernameIsTooLong = errors.New("username must be at most 32-character length")
ErrUsernameContainsInvalidChar = errors.New("username must contain only digits, underscores, dashes, dots and alphabetical letters")
ErrUsernameBeginsWithInvalidChar = errors.New("username must begin with either underscore or an alphabetical letter")
ErrEmailIsInvalid = errors.New("email is invalid")
ErrEmailIsTooLong = errors.New("email must be at most 128-character length")
ErrFullNameIsTooShort = errors.New("full name must be at least 1-character length")
ErrFullNameIsTooLong = errors.New("full name must be at most 128-character length")
@phuctm97
phuctm97 / interfaces.go
Created September 30, 2019 16:31
Go RESTful Series
package users
// UserRepository abstracts accesses to users-related data.
type UserRepository interface {
ExistsByUsername(string) (bool, error)
ExistsByEmail(string) (bool, error)
}
@phuctm97
phuctm97 / entities.go
Created September 30, 2019 16:34
Go RESTful Series
package users
// ValidateUserUnique validates whether user's username or email is valid and returns errors or nil.
func ValidateUserUnique(userRepo UserRepository, user *User) error {
errs := make([]error, 0)
var exists bool
var err error
// Validate username.
@phuctm97
phuctm97 / repositories.go
Created September 30, 2019 16:44
Go RESTful Series
package users
type mockUserRepository struct{}
func (r *mockUserRepository) ExistsByUsername(username string) (bool, error) {
if username == "existed" {
return true, nil
}
return false, nil
}
@phuctm97
phuctm97 / readerswriters.go
Last active October 1, 2019 15:12
Readers-Writers Problem with Go
package readerswriters
import "sync"
var mutex = new(sync.RWMutex)
func writer(data []byte) {
mutex.Lock()
defer mutex.Unlock()
@phuctm97
phuctm97 / syncval.go
Created October 1, 2019 15:40
Concurrent-safe value in Go
package syncval
import "sync"
// SyncValue is like a Go interface{} but is safe for concurrent use by multiple goroutines
// without additional locking or coordination.
type SyncValue struct {
mutex *sync.RWMutex
val interface{}
}
@phuctm97
phuctm97 / goroutines.go
Last active July 12, 2022 09:33
Manage Goroutines with context.Context
package main
import (
"context"
"fmt"
"os"
"os/signal"
)
func taskA(ctx context.Context, index int) {
@phuctm97
phuctm97 / 1asyncawait.js
Last active April 18, 2020 04:39
Use Go Channels as Promises and Async/Await
// Javascript.
const longRunningTask = async () => {
// Simulate a workload.
sleep(3000)
return Math.floor(Math.random() * Math.floor(100))
}
const r = await longRunningTask()
console.log(r)
@phuctm97
phuctm97 / 1asyncawait.js
Last active October 7, 2019 14:47
Use Go Channels as Promises and Async/Await
// Javascript.
const longRunningTask = async () => {
// Simulate a workload.
sleep(3000)
return Math.floor(Math.random() * Math.floor(100))
}
const [a, b, c] = await Promise.all(longRunningTask(), longRunningTask(), longRunningTask())
console.log(a, b, c)
@phuctm97
phuctm97 / 1asyncawait.js
Last active March 30, 2022 02:30
Use Go Channels as Promises and Async/Await
// Javascript.
const one = async () => {
// Simulate a workload.
sleep(Math.floor(Math.random() * Math.floor(2000)))
return 1
}
const two = async () => {
// Simulate a workload.