Wednesday, August 15, 2012

"datastore: invalid entity type"

The below error message is defined in datastore package of AppEngine.
ErrInvalidEntityType = errors.New("datastore: invalid entity type")
If by the accident one wrote:
data := Data{}
err := datastore.Get(c, key, data)

One will get above error message. The correct code looks as below:
data := Data{}
err := datastore.Get(c, key, &data)

The change is third passed parameter.

According to doc:
Get loads the entity stored for k into dst, which must be a struct pointer or implement PropertyLoadSaver.

Friday, August 10, 2012

Simple RegExp in Go

package main

import (
  "fmt"
  "regexp"
)

func main() {
  re := regexp.MustCompile("/page/(?P[\\d]+)/")
  b := []byte("/page/1/")
  x := re.Find(b)
  fmt.Printf("Found: `%s`\n", x)
  x1 := re.FindAll(b, 100)
  fmt.Printf("Found: `%s`\n", x1)
  fmt.Printf("Subexp num: `%d`\n", re.NumSubexp())
  x2 := re.FindAllSubmatch(b, 100)
  fmt.Printf("Found: `%s`\n", x2)
  fmt.Printf("Subexp names: %s", re.SubexpNames())
}

And output:
Found: `/page/1/`
Found: `[/page/1/]`
Subexp num: `1`
Found: `[[/page/1/ 1]]`
Subexp names: [ pagenr]
You can try it here: http://play.golang.org/p/SqUe4X7vuR