Skip to content

Instantly share code, notes, and snippets.

View soyart's full-sized avatar

Prem Phansuriyanon soyart

View GitHub Profile
@soyart
soyart / rust_read_from_file.rs
Last active June 24, 2021 17:00
Read file in Rust
fn read_vec(infile: &str) -> Vec<u8> {
fs::read(infile)
.expect("Failed to read file to vector")
}
// Or using '?' operator
fn read_byte(fname: &str) -> io::Result<Vec<u8>> {
let v = fs::read(fname)?;
Ok(v)
}
@soyart
soyart / hex.go
Created June 19, 2021 12:48
Encode/decode byte buffer to hex in Go
package main
/* Encoding/decoding byte buffer to hex */
import(
"io"
"os"
"bytes"
"encoding/hex"
)
@soyart
soyart / js_array_methods.js
Created June 21, 2021 19:23
JS array methods for noob like me
let arr = [
{ "name": "apple", "price": 10 },
{ "name": "banana", "price": 20 },
{ "name": "orange", "price": 30 }
];
// basic iteration
for (let i = 0; i < arr.length; i++) {
console.log(`name: ${arr[i].name}, price: ${arr[i].price}`);
};
@soyart
soyart / Clock.jsx
Last active June 24, 2021 17:14
SImple React ticking clock
import React from "react"
class Clock extends React.Component {
state = {
date: new Date()
}
componentDidMount() {
this.timerId = setInterval(() => {
@soyart
soyart / Todo.jsx
Created June 26, 2021 16:49
React todo list with local storage
import React, {useState, useEffect} from 'react'
export default function ToDo() {
const [todo, setTodo] = useState("");
// const [todos, setTodos] = useState([{id: 0, text: "default task"}]);
// Now it remembers saved Todo list
const [todos, setTodos] = useState(() => {
const savedTodos = localStorage.getItem("todos");
if (savedTodos) {
@soyart
soyart / spread.js
Created June 26, 2021 17:07
Spread and Rest operator
/* Spread: expands array into elements */
arr = [1, 2, 3];
arr1 = [...arr, 4]; // [1, 2, 3, 4]
/* Rest: condenses elements into array */
function multiply(multiplier, ...args) {
return args.map(num => num * multiplier);
}
multiply(2, 3, 4); // [6, 8]
@soyart
soyart / file_read_write.go
Created June 28, 2021 15:19
The many ways to read/write files in []byte or bytes.Buffer in Go
package main
/* The many ways to read/write files in Go */
import (
"bytes"
"io"
"os"
"fmt" // For performance testing
const arr = ["abc", "def", "xyz"];
function reverseArray(inArr) {
const result = new Array;
for (let i = inArr.length - 1; i >= 0; i--) {
result.push(inArr[i]);
}
return result;
}
@soyart
soyart / answers.md
Last active September 15, 2021 16:39
Answers for technical questions

1. When visiting a website, the following happens respectively

  1. DNS lookup

  2. TCP connection Open

  3. TLS handshake

  4. HTML parsing

@soyart
soyart / rsa.go
Last active February 4, 2023 07:44
RSA OAEP SHA256 encryption in Go
package main
import (
"crypto/rand"
"crypto/rsa"
"crypto/sha256"
"crypto/x509"
"encoding/base64"
"encoding/pem"