Add new types to Go

Posted by nevalu on Stack Overflow See other posts from Stack Overflow or by nevalu
Published on 2010-04-15T10:07:29Z Indexed on 2010/04/15 13:03 UTC
Read the original article Hit count: 390

Filed under:
|

I'm trying add new types for that been managed/used as in Go core types.

To create new types is anything very interesting to validate data before of send it to a non-SQL DBMS or to check data from a form.

  • Go uses univeral constants to define them at global level:

    var DateType = universe.DefineType("date", universePos, &dateType{})
    

    In this case they're defined to be called from a package like types:

    var Date = &dateType{}
    
  • I get these errors:

    test.go:58: o.lit undefined (cannot refer to unexported field lit)  
    test.go:62: *dateType is not Type  
      missing Pos() token.Position
    

The code is based on:
http://github.com/tav/go/blob/master/src/pkg/exp/eval/value.go http://github.com/tav/go/blob/master/src/pkg/exp/eval/type.go

package main

import (
    "exp/eval"
    "fmt"
//  "go/token"
)


// http://github.com/tav/go/blob/master/src/pkg/exp/eval/value.go

type DateValue interface {
    eval.Value
    Get(*eval.Thread) string
    Set(*eval.Thread, string)
}

/* Date */

type dateV string

func (v *dateV) String() string { return fmt.Sprint(*v) }

func (v *dateV) Assign(t *eval.Thread, o eval.Value) { *v = dateV(o.(DateValue).Get(t)) }

func (v *dateV) Get(*eval.Thread) string { return string(*v) }

func (v *dateV) Set(t *eval.Thread, x string) { *v = dateV(x) }


// http://github.com/tav/go/blob/master/src/pkg/exp/eval/type.go

type Type interface {
    eval.Type
    // isDate returns true if this is a date type.
    isDate() bool
}

/* Common type */

type commonType struct{}  // added

func (commonType) isDate() bool { return false }

/* Date */

type dateType struct {
    commonType
}

// * It should not be an universal constant

//var universePos = token.Position{"<universe>", 0, 0, 0}  // added
//var DateType = universe.DefineType("date", universePos, &dateType{})
var Date = &dateType{}

func (t *dateType) compat(o Type, conv bool) bool {
    t2, ok := o.lit().(*dateType)
    return ok && t == t2
}

func (t *dateType) lit() Type { return t }

func (t *dateType) isDate() bool { return true }

func (t *dateType) String() string { return "<date>" }

func (t *dateType) Zero() eval.Value {
    res := dateV("")
    return &res
}

/* Named types */
/*
type NamedType struct {
    eval.NamedType
    Def Type
}*/

type NamedType struct {  // added
//  token.Position
    Name string
    // Underlying type.  If incomplete is true, this will be nil.
    // If incomplete is false and this is still nil, then this is
    // a placeholder type representing an error.
    Def Type
    // True while this type is being defined.
    incomplete bool
    methods    map[string]eval.Method
}

func (t *NamedType) isDate() bool { return t.Def.isDate() }

/* *********************** */

func main() {
    print("foo")
}

© Stack Overflow or respective owner

Related posts about go

  • Go import error while trying to import web.go package after using goinstall

    as seen on Stack Overflow - Search for 'Stack Overflow'
    With halfdans advice, I was successfully able to use goinstall github.com/hoisie/web.go without any errors after installing git first. However, now when I try to compile the sample code given, go is not finding the web package. I get the error, main.go:4: can't find import: web On this code package… >>> More

  • Go Big or Go Home

    as seen on Oracle Blogs - Search for 'Oracle Blogs'
    The Oracle Develop conference (#oracledevelop10), being co-located for the first time ever with JavaOne in San Francisco, is guaranteed to be the ultimate rush for developers this year. Where else can you go to learn about, interact with, and meet fellow devotees of the entire Oracle Development… >>> More

  • Go Big or Go Home

    as seen on Oracle Blogs - Search for 'Oracle Blogs'
    For those who don’t know, Oracle sponsors a group called “OWL” – Oracle Women’s Leadership - and the purpose of the group is to create local and global opportunities that support, educate and empower current and future women leaders at Oracle. This week, I had the opportunity to attend the Denver… >>> More

  • Go Big or Go Special

    as seen on SQL Team - Search for 'SQL Team'
    Watching Shark Tank tonight and the first presentation was by Mango Mango Preserves and it highlighted an interesting contrast in business trends today and how to capitalize on opportunities.  <Spoiler Alert> Even though every one of the sharks was raving about the product samples they tried… >>> More

  • juju bootstrap fails with a local environment, why?

    as seen on Ask Ubuntu - Search for 'Ask Ubuntu'
    Each time I try to bootstrap juju using a local enviroment it fails starting the juju-db-braiam-local script as follows: $ sudo juju --debug --verbose bootstrap 2013-10-20 02:28:53 INFO juju.provider.local environprovider.go:32 opening environment "local" 2013-10-20 02:28:53 DEBUG juju.provider.local… >>> More

Related posts about datatypes