Skip to content

Instantly share code, notes, and snippets.

View shubhag's full-sized avatar

Shubham Agrawal shubhag

View GitHub Profile
@shubhag
shubhag / BUILD.bazel
Created October 11, 2021 12:25
Hello world golang with bazel: build file
load("@bazel_gazelle//:def.bzl", "gazelle")
# gazelle:prefix github.com/shubhag/bazel-golang-hello-world
gazelle(name = "gazelle")
@shubhag
shubhag / WORKSPACE
Created October 11, 2021 12:22
Hello world golang with bazel: workspace file
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
http_archive(
name = "io_bazel_rules_go",
sha256 = "2b1641428dff9018f9e85c0384f03ec6c10660d935b750e3fa1492a281a53b0f",
urls = [
"https://mirror.bazel.build/github.com/bazelbuild/rules_go/releases/download/v0.29.0/rules_go-v0.29.0.zip",
"https://github.com/bazelbuild/rules_go/releases/download/v0.29.0/rules_go-v0.29.0.zip",
],
)
@shubhag
shubhag / main.go
Created October 11, 2021 12:18
Hello world golang with bazel: go code
package main
import (
"fmt"
)
func main() {
fmt.Println("Hello world")
}
@shubhag
shubhag / customWriterFinal.go
Last active September 20, 2020 07:50
Custom writer working code
package main
import (
"bytes"
"encoding/json"
"io"
"os"
"os/exec"
"time"
)
@shubhag
shubhag / custom_writer_iter2.go
Created September 20, 2020 07:28
Custom writer iteration 2
type customWriter struct {
w io.Writer
}
func (e customWriter) Write(p []byte) (int, error) {
n, err := e.w.Write(p)
if err != nil {
return n, err
}
if n != len(p) {
@shubhag
shubhag / custom_writer_iter1.go
Last active September 20, 2020 07:25
Custom io.Writer iteration 1
type customWriter struct {
}
func (e customWriter) Write(p []byte) (int, error) {
// implement the functionality
}
@shubhag
shubhag / custom_writer.go
Last active September 20, 2020 07:44
Custom io.writer with extra debug information
type line struct {
Message string
Timestamp time.Time
Severity string
}
type customWriter struct {
w io.Writer
severity string
}
@shubhag
shubhag / run.go
Created September 20, 2020 06:45
Execute sub-process in golang
// This method execute a command in a sub-process and outputs the generated logs to a file.
func run(f *os.File) error {
args := []string{"-c", `echo "Standard output message"; >&2 echo "Standard error message"`}
cmd := exec.Command("bash", args...)
cmd.Stdout = f
cmd.Stderr = f
return cmd.Run()
}