Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 months ago.
Improve this question
I am just learning golangļ¼ json to struct,Get boolean value is always false,
if my json "remember":true, get boolean value is true,how to solve?
my code
package main
import (
"encoding/json"
"fmt"
)
type AdminInfoRequest struct {
Id uint `json:"id"`
UserName string `json:"username"`
Password string `json:"password"`
CaptchaId string `json:"captcha_id"`
Captcha string `json:"captcha"`
Remember bool `json:"remember"`
Status uint `json:"status"`
GroupId uint `json:"group_id"`
OldPassword string `json:"old_password"`
RePassword string `json:"re_password"`
}
func main() {
var s AdminInfoRequest
j := `{"username":"admin","remember":"true"}`
json.Unmarshal([]byte(j), &s)
fmt.Println(s.UserName)
fmt.Println(s.Remember)
}
In JSON, "true" is a string value. Try this:
j := `{"username":"admin","remember":true}`
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
Username is not required and it can be nil, so I made this property pointer to string.
type User struct {
Username *string `json:"username"`
Email string `json:"email"`
Password string `json:"password"`
}
func (u *User) PrepareUser() {
if u.Username != nil {
u.Username = html.EscapeString(strings.TrimSpace(u.Username))
}
u.Email = html.EscapeString(strings.TrimSpace(u.Email))
u.Password = strings.TrimSpace(u.Password)
}
When trying to trim and escape I see "cannot use html.EscapeString(strings.TrimSpace(u.Username)) (value of type string) as *string value in assignment"
You need to access the value of pointer via * operator.
html.EscapeString(strings.TrimSpace(*u.Username))
Update
Also don't forget to use * operator for assigning value
*u.Username = html.EscapeString(strings.TrimSpace(*u.Username))
func trimEscapeStrPtr(s *string) *string {
t := html.EscapeString(strings.TrimSpace(*s))
return &t
}
better example here
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I'm a beginner to Golang coming from JavaScript land. Is there a way to join one Golang struct into another?
Example:
type SimpleInfo struct {
name string,
age int,
}
type ComplexInfo struct {
SimpleInfo,
address string,
salary int,
}
Ideally, the intention is to make ComplexInfo look like this:
{
name string,
age int,
address string,
salary int,
}
You are on the right road, don't use commas
import (
"fmt"
)
type SimpleInfo struct {
name string
age int
}
type ComplexInfo struct {
SimpleInfo
address string
salary int
}
func main() {
fa:=ComplexInfo{}
fa.name="frank"
fa.salary=1000000
fmt.Println(fa.name, fa.salary)
}
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I'm trying to handle a struct in Go, which has two attributes that I need to be timestamps:
type Asset struct {
Owner string `json:"owner"`
Key string `json:"key"`
StartDate Time `json:"startDate"`
EndDate Time `json:"endDate"`
Type string `json:"type"`
Amount int `json:"amount"`
Facility string `json:"facility"`
State string `json:"state"`
}
I imported the "time" package, but the compiler gives me an error with the Time type:
"undeclared name: Time compiler UndeclaredName"
Any tips on why this is happening?
The Time type is defined in the time package. You have to import that package:
import "time"
And use a qualified identifier to refer to the exported Time type, which is packagename.Identifier, in this case time.Time:
type Asset struct {
Owner string `json:"owner"`
Key string `json:"key"`
StartDate time.Time `json:"startDate"`
EndDate time.Time `json:"endDate"`
Type string `json:"type"`
Amount int `json:"amount"`
Facility string `json:"facility"`
State string `json:"state"`
}
Try the working code on the Go Playground.
See related: Getting a use of package without selector error
It should be time.Time when you define struct with timestamp.
Refer to https://golang.org/pkg/time/ and time struct https://golang.org/src/time/time.go?s=6278:7279#L117
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I have a custom type named ProtectedCustomType and I don't want the variables within that to be set or get directly by the caller, rather want a Getter / Setter methods to do that.
Below is my ProtectedCustomType
package custom
import "fmt"
type ProtectedCustomType struct {
name string
age string
phoneNumber int
}
func (pct *ProtectedCustomType) SetAge (age string) {
pct.age=age
fmt.Println(pct.age)
}
func (pct *ProtectedCustomType) GetAge () string {
return pct.age
}
And here is my main function
package main
import (
"fmt"
"./custom"
)
var print =fmt.Println
func structCheck2() {
pct := custom.ProtectedCustomType{}
pct.SetAge("23")
age:=pct.GetAge
print (age)
}
func main() {
structCheck2()
}
I am expecting it to print 23, but it is printing as 0x48b950
This (your code) takes the pct instance's GetAge method and stores it in a variable:
age:=pct.GetAge
This calls the GetAge method and stores its return value in a variable:
age:=pct.GetAge()
Consider taking the Tour of Go and reading the Go Specification to get a basic understanding of Go syntax.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 5 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
I am trying to unmarshal json data. The slice inside is deliberately without quotes, because this is what I am getting from https (added manually \ before ")
data:="{\"queryEndTime\" : \"2017-11-15T14:39:00Z\", \"message\" : [{\"spamScore\":67,\"phishScore\":0}]}"
into Message struct:
type Message struct {
QueryEndTime string `json:"queryEndTime"`
Message []string `json:"message"`
}
but I am getting correct QueryEndTime and empty Message. I tried to change Message type but it always stays empty
var message Message
json.Unmarshal([]byte(data), &message)
fmt.Printf("QueryEndTime: %s\nMessage: %s\n", message.QueryEndTime, message.Message)
QueryEndTime: 2017-11-15T14:39:00Z
Message: []
See it in go playground https://play.golang.org/p/on0_cSKb0c.
package main
import (
"encoding/json"
"fmt"
)
type Message struct {
QueryEndTime string `json:"queryEndTime"`
// you need to use a struct can use anon struct
Message []struct {
SpamScore int `json:"spamScore"`
PhishScore int `json:"phishScore"`
} `json:"message"`
}
func main() {
var message Message
// You can use backticks to for your example JSON, so that you don't have to escape anything.
data := `{
"queryEndTime" : "2017-11-15T14:39:00Z",
"message" : [
{"spamScore":67, "phishScore":0}
]
}`
// please check for errors
err := json.Unmarshal([]byte(data), &message)
if err != nil {
fmt.Println(err)
}
// +v prints structs very nicely
fmt.Printf("%+v\n", message)
}
https://play.golang.org/p/Mu3WZCej3L
Have fun!