slice pointer to type Token []byte

55 views
Skip to first unread message

Gert

unread,
Dec 25, 2019, 10:05:07 PM12/25/19
to golang-nuts
Is there a cleaner way to write the 

s:= *t
*t = s[i+1:]

part?

was hoping for something like *t = *t[i+1:]


type Token []byte

func (t *Token) Next() bool {
if i := bytes.IndexByte(*t, '.'); i > -1 {
s := *t
*t = s[i+1:]
return true
}
return false
}

func (t Token) String() string {
if i := bytes.IndexByte(t, '.'); i > -1 {
return string(t[:i])
}
return string(t)
}

burak serdar

unread,
Dec 25, 2019, 10:33:23 PM12/25/19
to Gert, golang-nuts
On Wed, Dec 25, 2019 at 8:05 PM Gert <gert.c...@gmail.com> wrote:
>
> Is there a cleaner way to write the
>
> s:= *t
> *t = s[i+1:]
>
> part?
>
> was hoping for something like *t = *t[i+1:]

Indexing comes before indirection, so use parens:

*t=(*t)[i+1:]


>
>
> type Token []byte
>
> func (t *Token) Next() bool {
> if i := bytes.IndexByte(*t, '.'); i > -1 {
> s := *t
> *t = s[i+1:]
> return true
> }
> return false
> }
>
> func (t Token) String() string {
> if i := bytes.IndexByte(t, '.'); i > -1 {
> return string(t[:i])
> }
> return string(t)
> }
>
> --
> You received this message because you are subscribed to the Google Groups "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/5f59f852-99e5-472f-9857-cac893907ad6%40googlegroups.com.

Gert

unread,
Dec 25, 2019, 11:28:04 PM12/25/19
to golang-nuts


On Thursday, December 26, 2019 at 4:33:23 AM UTC+1, burak serdar wrote:
On Wed, Dec 25, 2019 at 8:05 PM Gert <gert....@gmail.com> wrote:
>
> Is there a cleaner way to write the
>
> s:= *t
> *t = s[i+1:]
>
> part?
>
> was hoping for something like *t = *t[i+1:]

Indexing comes before indirection, so use parens:

*t=(*t)[i+1:]


Thanks 
Reply all
Reply to author
Forward
0 new messages