Should i use DTO?

Hello

I am new to go lang and I want to use DTO. In your opinion , should I use DTO in my project?

Are you referring to the Data Transfer Object pattern ?

yes.do you suggest Data Transfer Object pattern? are there any library by implement dto pattern?

What the hell is DTO? How would you use it in your program?

In effect, this is none of business of go.

i use gin gonic framework and gorm in my arch. created gorm model but i dont want to use gorm model for web request bind operation or web response content. i create dummy model for web request and response but i am convert dummy model to gorm model (json convert). how can implement dto (Data Transfer Object) in my project in best way

1 Like

Just struct.

I recommend my toyorm orm, It very good at dot e.g

type User struct {
     ID int `toyorm:"primary key"`
     Name string
     Age int
     MetaData string 
}
// here Is web response data type
type UserName struct {
     ID int
     Name string

}

you just search data you want

DB,err := toyorm.Open("sqlite3", "xxx")
// error process
var data UserName{}
result,err := DB.Model(&User{}).Find(&data)
// process err & result.Err()

DTO is just an object carrying data for several requests. You just have to make struct and parse the request’s json into that. Afterwards can you iterate over the data and put it in correct structures. If the data sent in the DTO is of very different types it is maybe easiest to parse it as a map of strings to data and iterate over that. https://blog.golang.org/json-and-go

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.