How to trim and escape a string pointer in golang? [closed] - go

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

Related

golang json 'true' is no True? [closed]

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}`

Joining one Golang Struct into another [closed]

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)
}

Multiple receivers on a single method [closed]

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
Is it possible to have multiple receivers on a single function? In other words, a single function can belong to two or more structs?
Say I have
type Struct1 struct {
foo.Client
}
func CreateClient() struct1 {
return struct1{
ClientID: cId,
// ...
}
}
func (s *Struct1) MyFunc( // ... ) {}
But I also want to be able to associate MyFunc with another struct (different package):
type Struct2 struct {
lgr log.logger
}
func NewStruct2 (l *log.logger) (*Struct2, err) {
return &Struct2{mylog: *l}, nil
}
So what I want to actually have is:
func (s1 *Struct1, s2 *Struct2) MyFunc( // ... ) {}
"Is it possible to have multiple receivers on a single function?" -- It is not possible.
https://golang.org/ref/spec#Method_declarations
The receiver is specified via an extra parameter section preceding the
method name. That parameter section must declare a single non-variadic
parameter, the receiver.

Invalid receiver type with maps [closed]

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 years ago.
Improve this question
I'm trying to define additional methods on a basic map https://play.golang.org/p/3BKgxVJIjP1:
type Typ struct {
config string
}
type typeRegistry = map[string]Typ
func (r typeRegistry) Add(name string) {
typ := Typ{
config: "config",
}
r[name] = typ
}
Doing so fails:
invalid receiver type map[string]Typ (map[string]Typ is not a defined type)
Before refactoring the approach was similar but with a func instead of Typ:
type typeRegistry map[string]func()
func (r typeRegistry) Add(name string, factory func()) {
r[name] = factory
}
This version worked. Where is the difference in defining additional methods on map-type receivers?
type typeRegistry = map[string]Typ
Is a type alias. You cannot define methods on aliases (only on the original type--but in this case map[string]Typ cannot have methods on it, so you're out of luck).
What you probably want is to create a custom type, not an alias:
type typeRegistry map[string]Typ
Then your methods will work.

Protobuf/Go cannot use literal * (type *) as type * in array or slice literal [closed]

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 years ago.
Improve this question
I have a struct in my generated proto file that looks like this (simplified):
type Record struct {
Field1 int64 `protobuf:"varint,1,opt,name=field1,proto3" json:"field1,omitempty"`
Field2 []byte `protobuf:"bytes,2,opt,name=field2,proto3" json:"field2,omitempty"`
}
and I'm trying to call it in my Go file
func foo(c messagepb.MessageServiceClient){
fmt.Println("Starting to send message...")
msgs := []*messagepb.MessageRequest{
recordpb.Record{ //error msg here
Field1: 1,
Field2: []byte{byte('a')},
}
}
...
}
but I get this error at the recordpb.Record line:
cannot use recordpb.Record literal (type recordpb.Record) as type *messagepb.MessageRequest in array or slice literal
If it helps, here's my messagepb:
message.proto
message MessageRequest { recordpb.Record records = 1; }
message.pb.go
type MessageRequest struct {
Record *recordpb.Record `protobuf:"bytes,1,opt,name=record,proto3" json:"record,omitempty"`
}
I can't find anything useful about why this is happening... Any ideas?
Looks like you are creating a slice of []*messagepb.MessageRequest and then adding a recordpb.Record to it. recordpb.Record is not the same type as *messagepb.MessageRequest.
Given this type
type MessageRequest struct {
Record *recordpb.Record `protobuf:"bytes,1,opt,name=record,proto3" json:"record,omitempty"`
}
It looks like your msgs var should be
msgs := []*messagepb.MessageRequest{
{
Record: &recordpb.Record{
Field1: 1,
Field2: []byte{byte('a')},
},
},
}
#mkopriva 's comments above add some nice explanation to this.

Resources