Skip to content

Instantly share code, notes, and snippets.

@r002
r002 / fetch-ctwc-match.js
Last active August 14, 2021 01:48
JSON REST API call that doesn't return CORS
// Will work in Postman but not in jsfiddle or client-side/browser code!
const url = 'https://ctwc-cloud.appspot.com/v1/match/ctwc_2014_r5-01'
fetch(url).then((response) => {
if (response.ok) {
return response.json()
} else {
throw new Error('Something went wrong')
}
})
.then((responseJson) => {
@r002
r002 / gcloud_deploy_cmds.sh
Last active July 5, 2021 21:33
gcloud deploy commands
# PROD:
$> gcloud app deploy app.yaml --project studydash
$> gcloud functions deploy UpdateMemberMetrics --trigger-topic nightly_metrics_refresh --runtime go113 --env-vars-file ./cloudfunctions.yaml
# QA:
$> gcloud app deploy app.qa.yaml --project studydash-qa
@r002
r002 / dt_fmt_sandbox.go
Last active June 26, 2021 07:49
Formatting datetime strings in Go
// https://play.golang.org/p/6RkCCDQQ67Q
func main() {
loc, _ := time.LoadLocation("America/New_York")
t, _ := time.Parse(time.RFC3339, "2021-05-03T20:27:21Z")
fmt.Println(t.In(loc)) // 2021-05-03 16:27:21 -0400 EDT
fmt.Println(t.In(loc).Format(time.ANSIC)) // Mon May 3 16:27:21 2021
fmt.Println(t.In(loc).Format("2006-01-02")) // 2021-05-03
fmt.Println(t.In(loc).Format(time.RFC3339)) // 2021-05-03T16:27:21-04:00
}
@r002
r002 / go_commands.sh
Created June 16, 2021 20:08
Common Go Commands
$> go test .\ghservices -run Integration -v -count=1
@r002
r002 / gotest_relroot_readfiles.go
Created June 11, 2021 18:40
Change to ./ root in Go tests to read files (eg. go test .\service\ -v -count=1)
func init() {
_, filename, _, _ := runtime.Caller(0)
fmt.Println(">> Initializing testing:", filename)
dir := path.Join(path.Dir(filename), "..")
err := os.Chdir(dir)
if err != nil {
panic(err)
}
}
@r002
r002 / setenv.ps1
Created June 11, 2021 16:25
PowerShell Commands
ls env:
ls env: | findstr STUDYDASH
ls env: | findstr STUDYDASH_ENV
$env:STUDYDASH_ENV="qa"
$env:STUDYDASH_ENV=$null
@r002
r002 / array_example.go
Created June 8, 2021 17:26
Go Tutorial: Array example - Always passed by value
func TestHello(t *testing.T) {
fmt.Println(">> Array example - Always passed by value")
testcase1 := [1]struct {
arg string
want string
}{
{"original arg val", "original want val"},
}
fmt.Println(">> testcase1.arg:", testcase1[0].arg)
testcase2 := testcase1
@r002
r002 / slice_example.go
Created June 8, 2021 17:22
Go Tutorial: Slice example - Always passed by reference
func TestHello(t *testing.T) {
fmt.Println(">> Slice example - Always passed by reference")
testcase1 := []struct {
arg string
want string
}{
{"original arg val", "original want val"},
}
fmt.Println(">> testcase1.arg:", testcase1[0].arg)
testcase2 := testcase1
@r002
r002 / anonymous_struct.go
Last active June 8, 2021 17:15
Go Tutorial: Anonymous Structs
func TestHello(t *testing.T) {
testcase1 := &struct {
arg string
want string
}{
"original arg val",
"original want val",
}
fmt.Println(">> testcase1.arg:", testcase1.arg)
testcase2 := testcase1
@r002
r002 / pointers_test.go
Last active June 8, 2021 17:14
Go Tutorial: & - Pass by Reference
// https://stackoverflow.com/questions/33242850/in-golang-what-is-the-difference-between-and
type TestCase struct {
arg string
want string
}
func TestHello(t *testing.T) {
testcase1 := &TestCase{
arg: "original arg val",
want: "original want val",
}