Skip to content

Instantly share code, notes, and snippets.

View huantt's full-sized avatar
🎯
Focusing

Jack huantt

🎯
Focusing
View GitHub Profile
@huantt
huantt / growthbook-sample.go
Created October 20, 2023 08:18
Demo how to get experiment AB testing using growthbook.io service
package main
import (
_ "embed"
"fmt"
"github.com/growthbook/growthbook-golang"
)
func main() {
context := growthbook.NewContext()
@huantt
huantt / merge-go-models.go
Created October 20, 2023 02:07
Merge Golang Models
// MergeRecord
// if currentRecord.Field exists -> uses currentRecord.Field
// if currentRecord.Field exists AND newRecord.Field exists -> uses currentRecord.Field
// if currentRecord.Field does not exist AND newRecord.Field exists -> uses newRecord.Field
func MergeRecord[M any](currentRecord M, newRecord M) M {
currentValue := reflect.ValueOf(&currentRecord).Elem()
newValue := reflect.ValueOf(&newRecord).Elem()
for i := 0; i < currentValue.NumField(); i++ {
currentField := currentValue.Field(i)
[{"name":"Pacific/Niue","alternativeName":"Niue Time","group":["Pacific/Niue"],"continentCode":"OC","continentName":"Oceania","countryName":"Niue","countryCode":"NU","mainCities":["Alofi"],"rawOffsetInMinutes":-660,"abbreviation":"NUT","rawFormat":"-11:00 Niue Time - Alofi"},
{"name":"Pacific/Midway","alternativeName":"Samoa Time","group":["Pacific/Midway"],"continentCode":"OC","continentName":"Oceania","countryName":"United States Minor Outlying Islands","countryCode":"UM","mainCities":["Midway"],"rawOffsetInMinutes":-660,"abbreviation":"SST","rawFormat":"-11:00 Samoa Time - Midway"},
{"name":"Pacific/Pago_Pago","alternativeName":"Samoa Time","group":["Pacific/Pago_Pago","US/Samoa","Pacific/Samoa","Pacific/Midway"],"continentCode":"OC","continentName":"Oceania","countryName":"American Samoa","countryCode":"AS","mainCities":["Pago Pago"],"rawOffsetInMinutes":-660,"abbreviation":"SST","rawFormat":"-11:00 Samoa Time - Pago Pago"},
{"name":"Pacific/Rarotonga","alternativeName":"Cook Islands Time","group":["Pacif
@huantt
huantt / folder_structure.md
Created September 11, 2023 10:29 — forked from ayoubzulfiqar/folder_structure.md
The Folder Structure for Every Golang Project

Go - The Ultimate Folder Structure

Organizing your Go (Golang) project's folder structure can help improve code readability, maintainability, and scalability. While there is no one-size-fits-all structure, here's a common folder structure for a Go project:

project-root/
    ├── cmd/
    │   ├── your-app-name/
    │   │   ├── main.go         # Application entry point
    │   │   └── ...             # Other application-specific files
package main
type speaker struct {
}
func (s *speaker) Speak() string {
return "hello"
}
// Exported
@huantt
huantt / clone-all-repos-in-gitlab-group.sh
Created June 5, 2023 14:35
Clone all repositories in a gitlab group
host=https://git.your-domain.com \
session="a04dbb20e2c00433b3a5b137bc2c9b5c" \
group="your-group" \
api="$host/api/v4/groups/$group"
for repo in $(curl -s --header "cookie: _gitlab_session=$session" $api | jq -r ".projects[].ssh_url_to_repo"); do git clone $repo; done;
@huantt
huantt / postgres_queries_and_commands.sql
Last active June 9, 2023 02:46 — forked from rgreenjr/postgres_queries_and_commands.sql
Useful PostgreSQL Queries and Commands
-- show running queries (pre 9.2)
SELECT query_id, procpid, age(clock_timestamp(), query_start), usename, current_query
FROM pg_stat_activity
WHERE current_query != '<IDLE>' AND current_query NOT ILIKE '%pg_stat_activity%'
ORDER BY age desc;
-- show running queries (9.2)
SELECT query_id, pid, age(clock_timestamp(), query_start), usename, query
FROM pg_stat_activity
WHERE query != '<IDLE>' AND query NOT ILIKE '%pg_stat_activity%'
@huantt
huantt / golang_graphql_json_scalar.go
Created May 17, 2023 14:37
Json scalar implementation for github.com/graph-gophers/graphql-go
package graphql
import (
"encoding/json"
)
type Json map[string]interface{}
func (Json) ImplementsGraphQLType(name string) bool {
return name == "Json"
func GetGID() uint64 {
b := make([]byte, 64)
b = b[:runtime.Stack(b, false)]
b = bytes.TrimPrefix(b, []byte("goroutine "))
b = b[:bytes.IndexByte(b, ' ')]
n, _ := strconv.ParseUint(string(b), 10, 64)
return n
}
@huantt
huantt / golang-limit-requests-per-second.go
Last active May 12, 2023 09:10
Limit requests per second in Golang using time.Tick
package main
import (
"fmt"
"time"
)
func main() {
// Rate limit to 10 requests per second
for i := 0; i < 100; i++ {