Skip to content

Instantly share code, notes, and snippets.

View VinGarcia's full-sized avatar
💭
I may be slow to respond.

Vinícius Garcia VinGarcia

💭
I may be slow to respond.
  • Belo Horizonte - MG, Brazil
View GitHub Profile
@VinGarcia
VinGarcia / LICENSE
Last active August 31, 2022 13:30
Code for finding multiple free ports in Golang
Unlicense LICENSE
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.
In jurisdictions that recognize copyright laws, the author or authors
@VinGarcia
VinGarcia / calling-defer-close-at-the-wrong-time.go
Last active June 19, 2022 13:51
Golang SQL: Problems with existing libraries
rows, err := db.Query("...")
defer rows.Close()
if err != nil { ... }
for rows.Next() {
var user User
err = rows.Scan(...)
if err != nil { ... }
}
if rows.Err() != nil { ... }
@VinGarcia
VinGarcia / main.go
Created June 9, 2022 12:21
Short Example of Race Condition
package main
import (
"fmt"
"time"
)
func main() {
var v1, v2, v3 int
var sharedInt int
@VinGarcia
VinGarcia / add_date.go
Last active April 21, 2021 19:05
Implements an AddDate for Golang that works more like humans calculate dates (see issue in comment)
// This function had to be created because of an
// issue on the time.Time#AddDate() function:
//
// - https://github.com/golang/go/issues/31145
//
// where adding a month to Jan 30 would not get Feb 28
func addDate(date time.Time, y, m, d int) time.Time {
expectedMonth := (date.Month()+time.Month(m)-1)%12 + 1
date = date.AddDate(y, m, 0)
@VinGarcia
VinGarcia / README.md
Last active August 4, 2022 19:03
A script to wrap protoc & several dependencies in a portable way (meant to work with Golang)

This script was written to make it easy to compile protobuf files into Golang. It depends only on docker and bash which should make it very portable.

Usage

To run this script just copy it to your project (you can add it to version control) and then run it passing as argument the relative or absolute path of the directory containing your .proto files, e.g.:

bash protogen.sh my_proto_dir/
@VinGarcia
VinGarcia / iterating-by-reference-example.cpp
Last active November 6, 2018 01:51
Medium Article: Making Iterators in C++
for (auto& i : myContainer) {
cout << "My item: " << i << endl;
}
@VinGarcia
VinGarcia / walksync.js
Last active September 9, 2020 11:18 — forked from kethinov/walksync.js
// List all files in a directory in Node.js recursively in a synchronous fashion
var walkSync = function(dir, filelist) {
if( dir[dir.length-1] != '/') dir=dir.concat('/')
var fs = fs || require('fs'),
files = fs.readdirSync(dir);
filelist = filelist || [];
files.forEach(function(file) {
if (fs.statSync(dir + file).isDirectory()) {