George Opritescu

Developer from somewhere

embedding binary data as string literals

The problem: had a gzipped file that I wanted to include as a string literal in code.

The solution:

go-bindata -nocompress *.gz
  • found the []byte(“”) representation of the file
  • included it in the code
  • done!
Read More

import sql file

Found here

mysql -u username -p database_name < file.sql
Read More

mysql access denied for user root

Faced this error: Access denied for user ‘root’@’localhost’ (using password: YES) (Mysql2::Error)

Found the fix here:

$ mysql -u root mysql
$mysql> UPDATE user SET Password=PASSWORD('my_password') where USER='root';
$mysql> FLUSH PRIVILEGES;

but the 2nd variant was the one that worked in my case:

update user set authentication_string=password('my_password') where user='root';
Read More

golang Writer on []byte

Forgot to do a Flush on bufio.NewWriter, and the Bytes() method was returning an empty slice:

var buffer bytes.Buffer
writer := bufio.NewWriter(&buffer)
... do something with writer
// did not call <b>writer.Flush()</b>
data := writer.Bytes()
Read More
go

golang NewTimer and timing out a loop

package main

import "time"
import "fmt"

func main() {

	timer := time.NewTimer(time.Second * 1)
LOOP:
	for {
		select {
		case <-timer.C:
			fmt.Println("timing out")
			break LOOP
		default:
			fmt.Println("not timing out")
		}

	}
}
Read More

debug go tests with dlv

Useful info here:

  • run:
go test -c
  • then
dlv exec ./yourfile.test -- -test.run TestYourTestName
  • debug
Read More

atom exclude certain extensions when doing a file search

You can specify more than one pattern in the file/directory pattern, with ! where you can do something like this:

!vendor,!*.go

This will have the effect of not searching in the vendor folder, or in any file with .go extension.

Read More

pongo2 register custom function

Say you want to create a function that returns the value of an environment variable:

func get_env(name *pongo2.Value) *pongo2.Value {
	return pongo2.AsValue(os.Getenv(name.String()))
}

To use it in the templates, register it on the pongo2.Context:

context := pongo2.Context{}
context["get_env"] = get_env

Last step, is to pass that context when you render the view.

Read More

monitor files for changes and run command with nodemon

Used nodemon to restart a simple go web server, when changing any of the files from a folder called web:

nodemon --watch web --exec "go run main.go"
Read More

check non-standard extensions with facebook flow

By default, .js and .json are analyzed. Had some files with .es6 extension. To get them to show up when running flow, I had to add this to .flowconfig:

[options]
module.file_ext=.es6
module.file_ext=.js
module.file_ext=.json
Read More