Skip to content

curtank/go-explicit-type-conversion

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

go-explicit-type-conversion

explicit type conversion from one type to another

suppose you have 2 type of time the time.Time and timestamp.Timestamp

import (
	"fmt"
	"time"

	"github.com/curtank/go-explicit-type-conversion/client"
	"github.com/golang/protobuf/ptypes"
	"github.com/golang/protobuf/ptypes/timestamp"
)

type GoTimeStamp struct {
	CreateTime time.Time
	EndTime    time.Time
}
type GRPCTimeStamp struct {
	CreateTime *timestamp.Timestamp
	EndTime    *timestamp.Timestamp
}

func timetotimstamp(t time.Time) (*timestamp.Timestamp, error) {
	return ptypes.TimestampProto(t)
}
func main() {
	c := client.NewClient()
	c.AddFunc(timetotimstamp)
	gotime := GoTimeStamp{CreateTime: time.Now(), EndTime: time.Now()}
	grpctime := GRPCTimeStamp{}
	c.Convert(&gotime, &grpctime)
	fmt.Println(grpctime)
}

About

explicit type conversion from one type to another

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published