Skip to content

Instantly share code, notes, and snippets.

package main
import (
"fmt"
"log"
"net/http"
"os"
)
func handler(w http.ResponseWriter, r *http.Request) {
@jerryan999
jerryan999 / basic.go
Last active June 15, 2022 01:58
different golang error implementation
package main
import "errors"
import "fmt"
func bar() error {
return errors.New("an error")
}
func main() {
import { ObjectId } from 'mongodb'
const Mutation = {
createMovie: async (parent, args, { db }) => {
const newMovie = {
...args.input,
created_at: new Date()
}
const insertedResult = await db.collection('movies').insertOne(newMovie)
newMovie.id = insertedResult.insetedId
func DoReq() error {
resp, err := http.Get("http://localhost:8080/ping")
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
return errors.New("bad response")
}
@jerryan999
jerryan999 / buffer-channel.go
Last active May 5, 2022 14:57
context in Golang
ch := make(chan string, 1)
package main
import "fmt"
type User struct {
Name string
Age int
}
func (u User) String() string {
func MaxInt(args ...int) int {
if len(args) == 0 {
return *new(int)
}
max := args[0]
for _, arg := range args[1:] {
if arg > max {
max = arg
}
}
@jerryan999
jerryan999 / backgroud.py
Last active March 29, 2022 15:48
discord bot example
import discord
import asyncio
class MyClient(discord.Client):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
async def on_ready(self):
print(f'Logged in as {self.user} (ID: {self.user.id})')
@app.route("/create", methods=["POST"])
def create_book():
""""
Create a new book
"""
payload = request.get_json()
if "id" in payload: # user cannot pass id when creating a new book
payload.pop("id")
status = BookModel.Schema().validate(payload, partial=("id",)) # no validation to id
if status:
@jerryan999
jerryan999 / aes_128_ecb.go
Created February 17, 2022 09:08
AES-128-ECB PKCS7Padding
package main
import (
b64 "encoding/base64"
"fmt"
"github.com/andreburgaud/crypt2go/padding"
"golang.org/x/crypto/blowfish"
ecb "github.com/haowanxing/go-aes-ecb"