Skip to content

Instantly share code, notes, and snippets.

@mitchallen
mitchallen / server-post.go
Created February 21, 2023 10:32
Example of how to create a Gin POST url
r.POST("/users", func(c *gin.Context) {
var user struct {
Name string `json:"name"`
Email string `json:"email"`
}
if err := c.BindJSON(&user); err != nil {
c.JSON(400, gin.H{"error": err.Error()})
return
}
// TODO: create user in database
@mitchallen
mitchallen / server-get.go
Created February 21, 2023 10:30
Example of how to add a Gin GET url
r.GET("/users", func(c *gin.Context) {
users := []string{"Alice", "Bob", "Charlie"}
c.JSON(200, gin.H{
"users": users,
})
})
@mitchallen
mitchallen / server.go
Created February 21, 2023 10:28
Go (Golang) server using the Gin framework
/**
* Author: Mitch Allen
* File: server.go
*/
package main
import "github.com/gin-gonic/gin"
func main() {
@mitchallen
mitchallen / demo.go
Created March 22, 2022 04:01
How to use go-lib demo
package main
import (
"fmt"
"github.com/mitchallen/go-lib"
)
func main() {
fmt.Println(lib.Add(2, 3))
@mitchallen
mitchallen / README.md
Last active April 15, 2023 09:53
Example Go package README

github.com/mitchallen/go-lib

Usage

Initialize your module

go mod init example.com/my-golib-demo
@mitchallen
mitchallen / go-lib_test.go
Created March 22, 2022 02:45
go-lib test file
/**
* Author: Mitch Allen
* File: go-lib_test.go
*/
package lib
import (
"testing"
)
@mitchallen
mitchallen / go-lib.go
Created March 22, 2022 02:43
Example go package file
/**
* Author: Mitch Allen
* File: go-lib.go
*/
package lib
// Returns the sum of two numbers
func Add(a int, b int) int {
return a + b
@mitchallen
mitchallen / go-lib-init.go
Last active March 23, 2022 15:07
go-lib init.go
/**
* Author: Mitch Allen
* File: init.go
*/
package lib
import (
"fmt"
)
@mitchallen
mitchallen / app.js
Created January 13, 2022 03:12
Weighted choice app file
// Author: Mitch Allen
// File: app.js
import { weightedChoice } from './weighted-choice.js';
let canvas = document.getElementById("canvas");
const SCREEN_SIZE = 300;
const DIM = 10;
const CELL_SIZE = SCREEN_SIZE / DIM;
const BORDER = 1.0;
@mitchallen
mitchallen / app.css
Created January 13, 2022 03:10
Weighted choice stylesheet
canvas {
padding: 0;
margin: auto;
display: block;
width: 400px;
height: 400px;
position: absolute;
top: 0;
bottom: 0;
left: 0;