You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
28 lines
731 B
Go
28 lines
731 B
Go
package data
|
|
|
|
import (
|
|
"fmt"
|
|
"strconv"
|
|
)
|
|
|
|
// Declare a custom Runtime type, which has the underlying int32 type
|
|
type Runtime int32
|
|
|
|
// The rule about pointers vs. values for receivers is that value methods
|
|
// can be invoked on pointers and values, but pointer methods can only
|
|
// be invoked on pointers.
|
|
|
|
// Implements json.Marshaler interface.
|
|
func (r Runtime) MarshalJSON() ([]byte, error) {
|
|
// Generate a string containing the movie runtime
|
|
jsonValue := fmt.Sprintf("%d mins", r)
|
|
|
|
//Use strconv.Quote() function on the string to wrap it in double quotes
|
|
// necessary to be valid json
|
|
quotedJSONValue := strconv.Quote(jsonValue)
|
|
|
|
// convert the string to byte slice and return it
|
|
|
|
return []byte(quotedJSONValue), nil
|
|
}
|