type issue with EditIssueOption #282

Closed
opened 2020-03-04 12:48:58 +00:00 by noerw · 4 comments
Member

https://gitea.com/gitea/go-sdk/src/branch/master/gitea/issue.go#L172-L180

Is there a reason the string fields are pointers? This makes it hard / impossible to work with literals (I might be missing some golang syntax here?)

https://gitea.com/gitea/go-sdk/src/branch/master/gitea/issue.go#L172-L180 Is there a reason the string fields are pointers? This makes it hard / impossible to work with literals (I might be missing some golang syntax here?)
Owner

A pointer of string means it's optional, it maybe nil if there is no content.

A pointer of string means it's optional, it maybe `nil` if there is no content.
lunny added the
kind/question
label 2020-03-04 15:03:53 +00:00
Author
Member

Okay makes sense.
But how do I populate such a field? Creating a string pointer seems impossible:

opts := gitea.EditIssueOption{Body: "foo"}

gives cannot use "foo" (type string) as type *string in assignment

opts := gitea.EditIssueOption{Body: *"foo"}

gives invalid indirect of "foo" (type untyped string)

var body *string
body = "foo"

gives cannot use "foo" (type string) as type *string in assignment

...

Okay makes sense. But how do I populate such a field? Creating a string pointer seems impossible: ``` opts := gitea.EditIssueOption{Body: "foo"} ``` gives `cannot use "foo" (type string) as type *string in assignment` ``` opts := gitea.EditIssueOption{Body: *"foo"} ``` gives `invalid indirect of "foo" (type untyped string)` ``` var body *string body = "foo" ``` gives `cannot use "foo" (type string) as type *string in assignment` ...
Owner

It's not very convenient, but you can do like this.

var body = "foo"
opts := gitea.EditIssueOption{Body: &body}
It's not very convenient, but you can do like this. ```go var body = "foo" opts := gitea.EditIssueOption{Body: &body} ```
noerw closed this issue 2020-03-05 09:22:14 +00:00
Owner

@noerw or if you have to use this more often create a simple helper function:

https://play.golang.org/p/tzAdTv_Jn0U

func str2pt(s string) *string {
	return &s
}
@noerw or if you have to use this more often create a simple helper function: https://play.golang.org/p/tzAdTv_Jn0U ```go func str2pt(s string) *string { return &s } ```
Sign in to join this conversation.
No Milestone
No Assignees
3 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: gitea/go-sdk#282
No description provided.