Skip to content

Instantly share code, notes, and snippets.

View martinomburajr's full-sized avatar
🦆
Relentlessly pursuing those carbs

Martin Ombura Jr. martinomburajr

🦆
Relentlessly pursuing those carbs
View GitHub Profile
@martinomburajr
martinomburajr / create-gce-instance.go
Last active June 14, 2020 16:05
How to create a Google Compute Engine Instance using REST calls in Golang.
func CreateGCEInstance() {
//STEP 1 - INIT DATA - Add all your initialization data e.g. projectid, zones, accessTokens.
project := "<your-project-here>"
zone := "<your-zone-here>" //e.g. europe-west1-d
accessToken := "" ////retrieve from https://developers.google.com/oauthplayground/ select relevant compute engine scopes e.g. devstorage/read_write and auth/compute
endpoint := fmt.Sprintf("https://www.googleapis.com/compute/v1/projects/%s/zones/%s/instances", project, zone)
//STEP 2 - REQUEST BODY
//Create a REST representation of whats required. see https://cloud.google.com/compute/docs/reference/rest/v1/instances/insert
reqBody := struct {
@martinomburajr
martinomburajr / palindrome.asm
Created May 2, 2019 05:11
A small application written in Assembly language, that checks to see if an input string is a palindrome
#OMBMAR001
.data
isPalinMess: .asciiz "It is a palindrome \n"
noPalinMess: .asciiz "It is not a palindrome \n"
startMess: .asciiz "Enter a number: \n"
temp1: .word 0 # entered number
decima: .word 10 #decima
.text
@martinomburajr
martinomburajr / golang-os-windows-openfile.go
Last active May 13, 2019 04:30
Open file is a private function that showcases how Go Opens a file under the hood.
// Code Snippet 1
// os/file_windows.go
func openFile(name string, flag int, perm FileMode) (file *File, err error) {
r, e := syscall.Open(fixLongPath(name), flag|syscall.O_CLOEXEC, syscallMode(perm))
if e != nil {
return nil, e
}
return newFile(r, name, "file"), nil
}
@martinomburajr
martinomburajr / reading-files-using-file-descriptors.go
Created May 13, 2019 04:56
This is an example of how you can perform a low-level read using file descriptors in go. This will build a knowledge of how file descriptors work and will allow us to understand network connections a bit better
package main
import (
"log"
"syscall"
)
func main() {
//Open File to Retrieve Handler
fd, err := syscall.Open("<absolute-path-to-file>", syscall.O_RDWR, 755)
@martinomburajr
martinomburajr / netFD.go
Created May 13, 2019 13:27
The struct of a netFD or Network File System
// Network file descriptor.
type netFD struct {
pfd poll.FD
// immutable until Close
family int
sotype int
isConnected bool // handshake completed or use of association with peer
net string
laddr Addr
@martinomburajr
martinomburajr / netconn-anatomy.go
Last active May 14, 2019 05:08
The truncated anatomy of the net.Conn interface
// Conn is a generic stream-oriented network connection.
// Comments truncated for brevity
type Conn interface {
Read(b []byte) (n int, err error)
Write(b []byte) (n int, err error)
Close() error
LocalAddr() Addr
RemoteAddr() Addr
SetDeadline(t time.Time) error
@martinomburajr
martinomburajr / net-conn-read.go
Last active May 14, 2019 05:07
Reader implementation in the net.Conn
// Read implements the Conn Read method.
func (c *conn) Read(b []byte) (int, error) {
if !c.ok() {
return 0, syscall.EINVAL
}
n, err := c.fd.Read(b)
if err != nil && err != io.EOF {
err = &OpError{Op: "read",
Net: c.fd.net,
Source: c.fd.laddr,
@martinomburajr
martinomburajr / net-conn-write.go
Last active May 14, 2019 05:09
The Writer implementation of net.Conn
// Write implements the Conn Write method.
func (c *conn) Write(b []byte) (int, error) {
if !c.ok() {
return 0, syscall.EINVAL
}
n, err := c.fd.Write(b)
if err != nil {
err = &OpError{
Op: "write",
Net: c.fd.net,
@martinomburajr
martinomburajr / net-conn-close.go
Last active May 14, 2019 05:08
The Closer implementation of net.Conn
// Close closes the connection.
func (c *conn) Close() error {
if !c.ok() {
return syscall.EINVAL
}
err := c.fd.Close()
if err != nil {
err = &OpError{
Op: "close",
Net: c.fd.net,
@martinomburajr
martinomburajr / net-conn-struct.go
Created May 14, 2019 05:13
The net conn struct that has a pointer to a network file descriptor
type conn struct {
fd *netFD
}