2

I have this code

files, _ := ioutil.ReadDir("public/my-template/imagesT/gallery/")
    for _, f:=range files {
        fmt.Println(f.Name())
    }

How can return an array contain all f.Name to use them in index.html ?

1 Answer 1

2

Create a slice and use append to add the file names in your loop.

var fileNames []string
files, _ := ioutil.ReadDir("public/my-template/imagesT/gallery/")
for _, f := range files {
    fileNames = append(fileNames, f.Name())
}

// Now fileNames contains all of the file names for you to pass to your template.

Also note that you should not ignore the possible error returned on the line

files, _ := ioutil.ReadDir("public/my-template/imagesT/gallery/")
Sign up to request clarification or add additional context in comments.

1 Comment

i want to say that when i passed files in this code controller.carService.InsertImages(&fileNames) and the InsertImages function func (r CarService) InsertImages(images *[]string) { session, _ := mgo.Dial(r.uri) defer session.Close() session.SetSafe(&mgo.Safe{}) collection := session.DB(r.dbName).C(r.collectionName) for _, image := range images { error := collection.Insert( models.Car{ImagePath:image}) if error != nil { } } } i have error say : cannot range over images (type *[]string)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.