Skip to content

Instantly share code, notes, and snippets.

✍️
available for OSS Go packages

B.G.Adrian bgadrian

✍️
available for OSS Go packages
Block or report user

Report or block bgadrian

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@bgadrian
bgadrian / slicestricks.go
Created May 14, 2020
go slices more tricks
View slicestricks.go
// StringToImmutableBytes returns a slice of bytes from a string without allocating memory
// it is the caller's responsibility not to mutate the bytes returned.
func StringToImmutableBytes(s string) []byte {
if len(s) == 0 {
return nil
}
// NB(xichen): We need to declare a real byte slice so internally the compiler
// knows to use an unsafe.Pointer to keep track of the underlying memory so that
@bgadrian
bgadrian / docker.datadirectory
Created Nov 23, 2019
Docker change the data directory (Images, containers) on Ubuntu
View docker.datadirectory
According to the official documentation, as of Feb 2019, there are no --graph, -g options. These were renamed to the single argument --data-root.
vim /lib/systemd/system/docker.service so that the ExecStart takes into consideration that argument
ExecStart=/usr/bin/dockerd --data-root /mnt/data/docker -H fd://
systemctl daemon-reload
service docker restart
View bytes-to-string-no-memory.go
bytes := []bytes //string here, can be reused or kept on stack
stringVal := *(*string)(unsafe.Pointer(&bytes))
@bgadrian
bgadrian / publicip.sh
Created Jan 1, 2019
Get my public IP
View publicip.sh
dig @resolver1.opendns.com ANY myip.opendns.com +short
@bgadrian
bgadrian / hugo.yml
Created Dec 28, 2018
Hugo deployment to gitlab/github eg: .gitlab-ci.yml
View hugo.yml
image:
name: klakegg/hugo:0.53-ext-alpine
entrypoint: [""]
pages:
script:
- hugo
artifacts:
paths:
- public
@bgadrian
bgadrian / hugo_lazy_img.md
Last active May 1, 2020
Hugo lazy image loading
View hugo_lazy_img.md

I presume the images are bundled as post resources in the ./images/ folder.

It creates a new version at 5% of its original quality that is fetched in the initial page load. When and if the original image is loaded by the browser it will replace the low quality one.

{{< lazyimg "images/1.jpeg" >}} {{< lazyimg "images/1.jpeg" "caption" >}}

@bgadrian
bgadrian / .gitignore
Created Dec 19, 2018
Global git ignore for any system
View .gitignore
.idea/
.vs/
.env
.pk
.pem
.pub
@bgadrian
bgadrian / http.request_mock.go
Created Dec 15, 2018
Fake a HTTP Request/Client.Do response w/o a webserver.
View http.request_mock.go
type recordingTransport struct {
req *http.Request
}
func (t *recordingTransport) RoundTrip(req *http.Request) (resp *http.Response, err error) {
t.req = req
return &http.Response{
Body: ioutil.NopCloser(bytes.NewBufferString("alfa")),
}, nil
@bgadrian
bgadrian / Makefile
Last active Nov 5, 2018
Go 1.11 makefile example
View Makefile
# Makefile example from https://github.com/bgadrian/pseudoservice
# GO111MODULE is required only when inside GOPATH
source := ./cmd/pseudo-service-server/main.go
#this will be triggered before any command, or when just calling $ make
pre:
mkdir -p ./build/
env GO111MODULE=on go get -d ./
env GO111MODULE=on go test -race ./...
@bgadrian
bgadrian / app.yaml
Created Nov 4, 2018
App Engine for Go 1.11 servers (goswagger.io or custom)
View app.yaml
runtime: go111
service: myservice
env_variables:
HOST: "0.0.0.0"
instance_class: B1
basic_scaling:
max_instances: 2
idle_timeout: 15m
You can’t perform that action at this time.