Posts

Showing posts with the label go

GORM DB Connection on other package

GORM DB Connection on other package I start learning Go, reading about pointers, and want to split my database connection , and handler function for API. Already tried myself, by following this solution , but when i trying to read data, i am having this error [2018-06-26 21:59:45] sql: database is closed this is my source code. db.go package db import ( "fmt" "github.com/jinzhu/gorm" "github.com/joho/godotenv" "os" ) var Db *gorm.DB func Open() error { var err error _ = godotenv.Load(".env") dbType := os.Getenv("DB_TYPE") dbConnString := os.Getenv("DB_CONN_STRING") Db, err = gorm.Open(dbType, dbConnString) if err != nil { fmt.Println(err) } Db.LogMode(true) defer Db.Close() return err } func Close() error { return Db.Close() } person.go package model import ( "github.com/jinzhu/gorm" "github.com/gin-gonic/gin" "fmt" "namastra/gin/result...

cannot find vendor directory under golang project

cannot find vendor directory under golang project My golang version is go1.10.2 linux/amd64. I can build and run my go project(under gopath/src) without any problem but I cannot see vendor directory under my project folder. I would like to know if the vendor folder is a hidden directory? What are the possible reasons the vendor folder is not generated? Why would you expect a vendor directory? – tkausl Jul 1 at 3:45 "What are the possible reasons the vendor folder is not generated?" --- it's not done automatically. If you haven't done it explicitly - it's not created and used. Check github.com/golang/dep – zerkms Jul 1 at 3:51 Thank you! Will look...

Understanding interface inside interface(Embedded Interface)

Understanding interface inside interface(Embedded Interface) I was trying to understand the Interface embedding with the following code. I have the following: type MyprojectV1alpha1Interface interface { RESTClient() rest.Interface SamplesGetter } // SamplesGetter has a method to return a SampleInterface. // A group's client should implement this interface. type SamplesGetter interface { Samples(namespace string) SampleInterface } // SampleInterface has methods to work with Sample resources. type SampleInterface interface { Create(*v1alpha1.Sample) (*v1alpha1.Sample, error) Update(*v1alpha1.Sample) (*v1alpha1.Sample, error) Delete(name string, options *v1.DeleteOptions) error DeleteCollection(options *v1.DeleteOptions, listOptions v1.ListOptions) error Get(name string, options v1.GetOptions) (*v1alpha1.Sample, error) List(opts v1.ListOptions) (*v1alpha1.SampleList, error) Watch(opts v1.ListOptions) (watch.Interface, error) Patch(name strin...

Store and retrieve interfaces in golang

Store and retrieve interfaces in golang How can we store array of different structs into some file and retrieve it back in the same format without losing its properties(methods it provides). For example: I have data struct A and struct B , both implementing a common interface X {} with some methods. struct A struct B interface X {} One options is to write both save and retrieve method to accept the interface X slice. However the problem is how to unmarshal it back in some generic way which is not tied to my Data struct. i.e., every time I add a new data struct I need not to change my save or retrieve functions to retrieve back the slice of interface X so that its methods can be used independent of data struct. Example where Unmarshaling throws error : Go PlayGround Link with a small Example 3 Answers 3 However the problem is how to unmarshal it back in some generic way which is not tied to my Data...

Parsing dynamic json in go

Parsing dynamic json in go I am trying to parse the following json structure, where the fields marked with "val1" and "val2" are constantly changing, so I cannot use a predefined struct. How could I parse this json in a way to be able to loop through every single "val"? Thank you! {"result":true,"info":{"funds":{"borrow":{"val1":"0","val2":"0"},"free":{"val1":"0","val2":"0"},"freezed":{"val1":"0","val2":"0"}}}} struct { Borrow, Free, Freezed map[string]interface{} } – ThunderCat Jun 30 at 20:09 struct { Borrow, Free, Freezed map[string]interface{} } Possible duplicate of stackoverfl...

zip folder with source and target

zip folder with source and target I use the following code which zip some folder to a given path,the issue that im currently facing is that I need to zip some folder with content into specific target and not in the same directory For example Folder in path like source "/Users/i03434/go/src/zdf/BUILD" "/Users/i03434/go/src/zdf/BUILD" target "/Users/i03434/go/src/zdf/app/info.zip" "/Users/i03434/go/src/zdf/app/info.zip" currently I try to add new path[2] which doesnt helps, any idea how to do it? This is all the code func zipit(params ...string) error { zipfile, err := os.Create(params[1]) if err != nil { return err } defer zipfile.Close() archive := zip.NewWriter(zipfile) defer archive.Close() info, err := os.Stat(params[0]) if err != nil { return err } var baseDir string if info.IsDir(); len(params) > 2 { baseDir = params[2] } else { baseDir = filepath.Base(params[...