1

I have below code. I am trying to convert from string value to time format and assigning the value and failing with below error at line no:

cannot assign time.Time to psdvalue (type string) in multiple assignment

Code:

type Tracking struct {
    release_name       string
    planned_start_date time.Time
}

const layout = "01-02-2006"

func saveHandler(response http.ResponseWriter, request *http.Request) {

    releasevalue := request.FormValue("ReleaseName")
    psdvalue := request.FormValue("PSD")

    if len(strings.TrimSpace(psdvalue)) > 0 {
        //line no:              psdvalue, _ = time.Parse(layout, psdvalue)
    }

    array = append(array, Tracking{
        release_name:       releasevalue,
        planned_start_date: psdvalue,
    })

}

3 Answers 3

2

In your case error happens because you are using same var for 2 types, if you change psdvalue to something else it will work. Check here - https://play.golang.org/p/Z8_--GluMoP

package main

import (
    "fmt"
    "time"
)

func main() {
    layout := "01-02-2006"
    psdvalue := "04-04-2004"
    parsed, err := time.Parse(layout, psdvalue)
    if err != nil {
        panic(err)
    }
    fmt.Printf("%v", parsed)
}

Also, do not forget to handle the error in Parse function.

Sign up to request clarification or add additional context in comments.

Comments

1

time.Parse returns a time and an error. You are assigning the string portion to pdsvalue which is already declared as a string when you assign the FormValue of "PSD". psdvalue is therefore already a string type and cannot be assigned a time.Time value. Use a different variable name in your assignment (and don't swallow the error either).

Comments

0

You have to take both returned values:

const shortForm = "2006-Jan-02"
t, _ = time.Parse(shortForm, "2013-Feb-03")

https://golang.org/pkg/time/#Parse

Comments

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.