π¨βπ»
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package users | |
// UserRepository abstracts accesses to users-related data. | |
type UserRepository interface { | |
ExistsByUsername(string) (bool, error) | |
ExistsByEmail(string) (bool, error) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package users | |
type mockUserRepository struct{} | |
func (r *mockUserRepository) ExistsByUsername(username string) (bool, error) { | |
if username == "existed" { | |
return true, nil | |
} | |
return false, nil | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package readerswriters | |
import "sync" | |
var mutex = new(sync.RWMutex) | |
func writer(data []byte) { | |
mutex.Lock() | |
defer mutex.Unlock() | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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{} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"context" | |
"fmt" | |
"os" | |
"os/signal" | |
) | |
func taskA(ctx context.Context, index int) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Javascript. | |
const longRunningTask = async () => { | |
// Simulate a workload. | |
sleep(3000) | |
return Math.floor(Math.random() * Math.floor(100)) | |
} | |
const r = await longRunningTask() | |
console.log(r) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Javascript. | |
const one = async () => { | |
// Simulate a workload. | |
sleep(Math.floor(Math.random() * Math.floor(2000))) | |
return 1 | |
} | |
const two = async () => { | |
// Simulate a workload. |