try to fill slice with struct and method in golang - go

I want to create a struct movie with the property title and genre data type string, then duration and year data type integer
then create a function with the name addDataFilm to add the data object from the struct to the dataFilm slice, then display the data:
this is my code :
type movie struct {
title, genre string
duration, year int
}
func (m movie) addDataFilm(title string, genre string, duration int, year int, dataFilm *[]string) {
var d = strconv.Itoa(m.duration)
var y = strconv.Itoa(m.year)
*dataFilm = append(*dataFilm, m.title, m.genre, d, y)
}
func main(){
var dataFim = []string{}
var dd = movie{}
dd.addDataFilm("LOTR", "action", 120, 1999, &dataFim)
dd.addDataFilm("Avanger", "action", 120, 2004, &dataFim)
dd.addDataFilm("Spiderman", "action", 120, 2004, &dataFim)
dd.addDataFilm("Juon", "horror", 120, 2004, &dataFim)
fmt.Println(dataFim)
}
all i got is :
any help will be appreciated. thank you in advance

Create a slice of movies. Append each movie to the slice. To print, range over the slice and print each movie.
var movies []*movie
movies = append(movies, &movie{"LOTR", "action", 120, 1999})
movies = append(movies, &movie{"Avanger", "action", 120, 2004})
movies = append(movies, &movie{"Spiderman", "action", 120, 2004})
movies = append(movies, &movie{"Juon", "horror", 120, 2004})
for i, m := range movies {
fmt.Printf("%d. Title: %s\n Genre: %s\n Duration: %d\n Year: %d\n\n", i+1, m.title, m.genre, m.duration, m.year)
}
Run the program on the playground.
The logic can be wrapped up in a type:
// dataFilms stores data for multiple films.
type dataFilms []*movie
func (df *dataFilms) add(title string, genre string, duration int, year int) {
*df = append(*df, &movie{title, genre, duration, year})
}
func (df dataFilms) print() {
for i, m := range df {
fmt.Printf("%d. Title: %s\n Genre: %s\n Duration: %d\n Year: %d\n\n", i+1, m.title, m.genre, m.duration, m.year)
}
}
func main() {
var df dataFilms
df.add("LOTR", "action", 120, 1999)
df.add("Avanger", "action", 120, 2004)
df.add("Spiderman", "action", 120, 2004)
df.add("Juon", "horror", 120, 2004)
df.print()
}
Run it on the playground.

create struct new array of Movies
create global variable Movies to save data
call variable on func main
type Movie struct {
Title string
Genre string
Duration int
Year int
}
type Movies []Movie
var dest Movies
func addDataFilm(title string, genre string, duration int, year int) Movies {
dest = append(dest, Movie{
Title: title,
Genre: genre,
Duration: duration,
Year: year,
})
return dest
}
func TestNumberToAlphabet(t *testing.T) {
addDataFilm("LOTR", "action", 120, 1999)
addDataFilm("Avanger", "action", 120, 2004)
addDataFilm("Spiderman", "action", 120, 2004)
addDataFilm("Juon", "horror", 120, 2004)
fmt.Println(dest)
}

Related

mapstructure json into a structure with nested dictionary

I am new to golang. I have a json file with nested structures that I want to parse and populate.
I am trying to use mapstructure to try and populate. I am able to do it for simple structures. But when it comes to array for dictionaries(key:struct). The map[string]interface{} seems to fail with runtime error: index out of range.
I tried to do the following for the json example below.
type Window struct {
loc []int
wrtc string
label string
}
type View struct {
windows []Window
}
type Views struct {
views []View
}
type Desktop struct {
views []Views `mapstructure:views`
rotation_speed string `mapstructure:"rotationSpeed" json:rotationSpeed"`
}
func main() {
file, _ := ioutil.ReadFile("test.json")
data := Desktop{}
_ = json.Unmarshal([]byte(file), &data)
fmt.Println("data: ", data.views[0])
}
{
"desktop": {
"view": [{// configs for view1
"random_id1": {
"loc": [0,0,640,360],
"wrtc": "some string",
"label": "window 1"
},
"random_id213443": {
"loc": [640,360,1280,720],
"wrtc": "some string blah",
"label": "window 2"
},
// more windows with random ids....
},
{
// configs for view2...
}
],
"rotationSpeed": 30
}
Since the window id is random I am not able to define it in a struct.
I tried using mapstructure:",squash" but that seems to fail as well.
I appreciate any help with this.
#Burak Serdar is right
You don't need mapstructure. JSON unmarshaling can deal with this.
you code have many error, like struct, uppercase, 'views' etc..
follow is a demo:
package main
import (
"encoding/json"
"fmt"
)
var data = `
{
"desktop":{
"view":[
{
"random_id1_1":{
"loc":[
0,
0,
640,
360
],
"wrtc":"some string",
"label":"window 1"
},
"random_id1_2":{
"loc":[
640,
360,
1280,
720
],
"wrtc":"some string blah",
"label":"window 2"
}
},
{
"random_id2_1":{
"loc":[
0,
0,
640,
360
],
"wrtc":"some string",
"label":"window 1"
},
"random_id2_2":{
"loc":[
640,
360,
1280,
720
],
"wrtc":"some string blah",
"label":"window 2"
}
}
],
"rotationSpeed":30
}
}
`
type Window struct {
Loc []int
Wrtc string
Label string
}
type Desktop struct {
View []map[string]Window
Rotation_speed int `json:"rotationSpeed" mapstructure:"rotationSpeed"`
}
type Config struct {
Desktop Desktop
}
func main() {
c := Config{}
json.Unmarshal([]byte(data), &c)
fmt.Println("json.Unmarshal: ", c)
}
json.Unmarshal: {{[map[random_id1_1:{[0 0 640 360] some string window 1} random_id1_2:{[640 360 1280 720] some s
tring blah window 2}] map[random_id2_1:{[0 0 640 360] some string window 1} random_id2_2:{[640 360 1280 720] some
string blah window 2}]] 30}}
also you can use mapstructure by "remain", if you want View struct
type Window struct {
Loc []int
Wrtc string
Label string
}
type View struct {
Windows map[string]Window `mapstructure:",remain"`
}
type Desktop struct {
View []View
Rotation_speed int `json:"rotationSpeed" mapstructure:"rotationSpeed"`
}
type Config struct {
Desktop Desktop
}
func main() {
c2 := Config{}
m := map[string]interface{}{}
_ = json.Unmarshal([]byte(data), &m)
mapstructure.Decode(m, &c2)
fmt.Println("mapstructure: ", c2)
}

How to implement repository in Go in case of complex requests?

Suppose we have multiple products with next fields: id, name, type, price, weight.
I want to return products that match some complex filter, for example:
name like '%bla%' and type = 3 - return all products that contains specific substring in name and belongs to specific type
name like '%bla%' and type=4 and weight/price < 10
(name like '%bla%' and type=5) or (name like '%lala%' and type=6 and price < 200)
I don't want to implement separate method for every possible filter.
Repository fetches data from db (I use postgres).
You can do like this:
package main
import(
"fmt"
"regexp"
)
type Items struct{
_id string;
_name string;
_type int;
_price float64;
_weight float64;
}
func (i *Items) filter(bla string) []Items{
items := []Items{
Items{
_id: "i_78676758",
_name: "packet1",
_type: 4,
_price: 44.65,
_weight: 3.6,
},
Items{
_id: "i_546458",
_name: "packet2",
_type: 5,
_price: 234.65,
_weight: 123.6,
},
Items{
_id: "i_5879788",
_name: "packet2",
_type: 5,
_price: 34.65,
_weight: 13.6,
},
Items{
_id: "i_7858758",
_name: "packet3",
_type: 3,
_price: 284.65,
_weight: 23.6,
},
};
var validID = regexp.MustCompile(regexp.QuoteMeta(bla))
new_items := []Items{}
for _, item := range items{
switch{
case ((validID.MatchString(item._name)) && (item._type == 3)):
new_items = items
break;
case ((validID.MatchString(item._name)) && (item._type == 4) && (item._price < 10) && (item._weight < 10)):
new_items = append(new_items, item)
case (((validID.MatchString(item._name)) && item._type == 5) || ((validID.MatchString(item._name)) && (item._type == 6) && (item._price < 200))):
new_items = append(new_items, item)
case ((validID.MatchString(item._name)) && (item._price > 100)):
new_items = append(new_items, item)
default:
}
}
return new_items;
}
func main(){
item := &Items{
_id: "i_7858758",
_name: "packet",
_type: 4,
_price: 234.65,
_weight: 23.6,
}
items := item.filter("et2")
fmt.Println(items)
}
In the above code Items struct holds the data which has filter method which loops over items which is a []Items and filter on the basis of text which is matched by Regexp and switch case to validate other things and finally returns a []Items which holds the filtered data.

How to properly structure struct in struct

I started to learn Go and my (self assigned) mission is to print out a list of hard-coded albums and songs which are on this album.
I've created struct Album which is "object" of album. Inside this struct Album I want to create underlying struct Song. This "object" will hold songs of that album.
My problem/error that I'm getting is ./main.go:46:4: cannot use []Song literal (type []Song) as type Song in field value
This is my code.
// Song struct
type Song struct {
SongName string `json:"song_name"`
Position int `json:"position"`
Length string `json:"length"`
}
// Album struct
type Album struct {
Name string `json:"name"`
Artist string `json:"artist"`
YouTubeLink string `json:"youtube_link"`
AlbumImage string `json:"album_image"`
Description string `json:"description"`
ReleaseDate string `json:"release_date"`
RecordDate string `json:"record_date"`
Length string `json:"length"`
Studio string `json:"studio"`
Songs Song `json:"songs"`
}
func listOfAlbums() []Album {
a := []Album{
{
Name: "The Dark Side of the Moon",
Artist: "Pink Floyd",
YouTubeLink: "https://www.youtube.com/watch?v=HW-lXjOyUWo&list=PLEQdwrAGbxnc9lyYltGRUMt3OgfX0tFbd&index=1",
AlbumImage: "https://upload.wikimedia.org/wikipedia/sl/b/bb/Dsotm.jpg",
Description: "The Dark Side of the Moon is the eighth studio album by English rock band Pink Floyd, released on 1 March 1973 by Harvest Records. Primarily developed during live performances, the band premiered an early version of the record several months before recording began.",
ReleaseDate: "1973.03.01",
RecordDate: "1972.06.01 – 1973.01.01",
Length: "42:32",
Studio: "Abbey Road Studios, London",
//
// Problematic part of code
//
Songs: []Song{
{
SongName: "Speak to Me / Breathe",
Position: 1,
Length: "1:13",
},
{
SongName: "Breathe",
Position: 2,
Length: "2:43",
},
{
SongName: "On the run",
Position: 3,
Length: "3:36",
},
{
SongName: "Time",
Position: 4,
Length: "6:53",
},
{
SongName: "The Great Gig in the Sky",
Position: 5,
Length: "4:36",
},
{
SongName: "Money",
Position: 6,
Length: "6:23",
},
{
SongName: "Us and Them",
Position: 7,
Length: "7:49",
},
{
SongName: "Any Colour You Like",
Position: 8,
Length: "3:26",
},
{
SongName: "Brain Damage",
Position: 9,
Length: "3:49",
},
{
SongName: "Eclipse",
Position: 10,
Length: "2:03",
},
},
},
}
return a
}
Here is a working Go PlayGround code
The questions are following:
Is my logic going in to the right direction? Do I even need to create another struct (Song) or this could be solved in some better manner?
How should I restructure my code so it will be working?
In your Album struct, a single Song is expected for the Songs field. However, []Song is a slice (list type) of songs. When you update your struct declaration with []Song instead of Song, your definition is good.
type Album struct {
Name string `json:"name"`
Artist string `json:"artist"`
YouTubeLink string `json:"youtube_link"`
AlbumImage string `json:"album_image"`
Description string `json:"description"`
ReleaseDate string `json:"release_date"`
RecordDate string `json:"record_date"`
Length string `json:"length"`
Studio string `json:"studio"`
Songs []Song `json:"songs"`
}
Now you can use a []Song{...} for this field, which is also expected in your data definition.
Your code is totally fine and it is normal to use multiple structs for bigger objects that contain other objects.

Remove Element From Struct But Only For This One Function

So I have an Struct that holds data that has a AddedByUser which links to my User Struct.
What I want to be able to do it remove the UserLevel from the AddedByUser
Now I want to be able to do it from this function only, so using the json:"-" is not an option. That would remove it from all json output. I only want to remove it form this one function.
I should also say that these are Gorm models and when I have been trying to remove the 10 option (UserLevels) it only removes the outer data set not the UserLevel from all of the data.
{
"ID": 1,
"CreatedAt": "2019-01-08T16:33:09.514711Z",
"UpdatedAt": "2019-01-08T16:33:09.514711Z",
"DeletedAt": null,
"UUID": "00000000-0000-0000-0000-000000000000",
"Title": "title000",
"Information": "info999",
"EventDate": "2006-01-02T15:04:05Z",
"AddedByUser": {
"ID": 2,
"CreatedAt": "2019-01-08T15:27:52.435397Z",
"UpdatedAt": "2019-01-08T15:27:52.435397Z",
"DeletedAt": null,
"UUID": "b019df80-a7e4-4397-814a-795e7e84b4ca",
"Firstname": "Me",
"Surname": "admin",
"Password": "....",
"Email": "admin#email.co.uk",
"UserLevel": {
"ID": 0,
"CreatedAt": "0001-01-01T00:00:00Z",
"UpdatedAt": "0001-01-01T00:00:00Z",
"DeletedAt": null,
"LevelTitle": "",
"UserLevel": null
},
So this is what I have tried,
data := []models.MyData{}
data = append(data[0:2])
I have about 14 results, with out the append it loads all the results but with this is only loads two results. The idea was to remove either UpdateAt or Title. As I am not sure if the gorm model information is all 0 or if the slice sees them as 0,1,2,3,4 etc.
I have also tried to range over the slice of models, while I can access each of the sections, I can not seem to find a simple method to remove data by name from a struct? Maps seem to have that but not structs which I am not sure why?
Thanks.
UPDATE
This is the model I am using:
//Model
type MyData struct {
gorm.Model
UUID uuid.UUID
Title string
Information string
EventDate time.Time
AddedByUser Users `gorm:"ForeignKey:added_by_user_fk"`
AddedByUserFK uint
}
//Users Model
type Users struct {
gorm.Model
UUID uuid.UUID
Firstname string
Surname string
Password string
Email string
UserLevel UserLevels `gorm:"ForeignKey:user_level_fk" json:",omitempty"`
UserLevelFK uint
}
As mentioned in the comments, you cannot remove fields from a struct value, because that would yield a value of a different type.
However, you can set fields to their zero value. Combined with the omitempty JSON tag, you can exclude fields from the JSON encoding. To make this work properly, you have to change the UserLevel field to a pointer type (otherwise you end up with empty objects in the JSON document).
Types shortened for brevity:
package main
import (
"encoding/json"
"fmt"
)
type MyData struct {
Title string
AddedByUser Users
}
type Users struct {
ID int
UserLevel *UserLevels `json:",omitempty"` // pointer type with omitempty
}
type UserLevels struct {
LevelTitle string
}
func main() {
var x MyData
x.Title = "foo"
x.AddedByUser.ID = 2
x.AddedByUser.UserLevel = &UserLevels{}
f(x)
b, _ := json.MarshalIndent(x, "", " ")
fmt.Println("main:\n" + string(b))
}
func f(x MyData) {
// "unset" UserLevel. Since we are receiving a copy of MyData, this is
// invisible to the caller.
x.AddedByUser.UserLevel = nil
b, _ := json.MarshalIndent(x, "", " ")
fmt.Println("f:\n" + string(b))
}
// Output:
// f:
// {
// "Title": "foo",
// "AddedByUser": {
// "ID": 2
// }
// }
// main:
// {
// "Title": "foo",
// "AddedByUser": {
// "ID": 2,
// "UserLevel": {
// "LevelTitle": ""
// }
// }
// }
Try it on the playground: https://play.golang.org/p/trUgnYamVOA
Alternatively, you can define new types that exclude the AddedByUser field. However, since this field isn't at the top level, this is a lot of work, and it's easy to forget to update those types when new fields are added to the original types.
If the field were at the top level, the compiler would do most of the work for you, because types that only differ in their field tags can be directly converted to one another:
type MyData struct {
ID int
Title string
}
func main() {
var x MyData
x.ID = 1
x.Title = "foo"
f(x)
}
func f(x MyData) {
type data struct { // same as MyData, except the field tags
ID int
Title string `json:"-"`
}
b, _ := json.MarshalIndent(data(x), "", " ")
fmt.Println("main:\n" + string(b))
}

Cannot convert value of type '(year: Int, month:Int,day:int)' to expected argument type 'String'

I was making a little program to make a family with classes and suddenly i got the error Cannot convert value of type '(year: Int, month:Int,day:int)' to expected argument type 'String'. I'm still thinking but i'm not sure what's the matter. Can anyone help? (I'm using the language Swift 2)
import UIKit
class persone {
var age : Int = 1
let name : String
var height : Double = 1.6
let hairColor : String
var birthday : (year: Int,month : Int, day : Int) = (2007,12,18)
init(age: Int, name: String, height: Double, hairColor: String, birthday: String){
self.age = age
self.name=name
self.height = height
self.hairColor = hairColor
let a = birthday.componentsSeparatedByString(",")
self.birthday = (Int(a[0])!,Int(a[1])!,Int(a[2])!)
}
func howOld()->Int{
return age-birthday.year
}
}
class grandma : person{
let sex : String = "female"
init(name : String, birthday : String, height :Double ,hairColor : String ) {
super.init(age: self.age, name: self.name, height: self.height, hairColor: self.hairColor, birthday: self.birthday)
}
}
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
The error is in the class 'Grandma' under where it says let sex : String = "female"

Resources