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 / install-macos.sh
Last active April 17, 2020 08:55
πŸ’» Install macOS Catalina
#!/bin/bash
# Enable sudo Touch ID authentication.
sudo python <<HEREDOC
import re
pam_cfg = '/etc/pam.d/sudo'
auth_re = re.compile(r'^auth\s+sufficient\s+')
tid_re = re.compile(r'^auth\s+sufficient\s+pam_tid.so')
@phuctm97
phuctm97 / cgo.go
Created October 25, 2019 11:51
CGO Memory Map
package main
/*
#include <stdlib.h>
typedef int cgo_ptr;
*/
import "C"
import "sync"
@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.
@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 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 / 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 / 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 / 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 / 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 / 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.