Checking length of a string pointer in Go

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP

Checking length of a string pointer in Go



How do I go about finding the length of a string pointer which comes from a struct. Currently I am writing unit tests and want to see whether a string pointer is less than 250 characters. If r is my and myString is a string pointer.



This code is not valid:


if len(r.myString) > 256
return ErrStringTooLong





String pointers are always the same size--the size of a pointer.
– Flimzy
2 days ago




2 Answers
2



You should explain why your code is not correct, and which error message you get. If it is a pointer to a string, you should dereference it by asterix *


len(*r.myString)



you can access an object with a star before pointer variable name for eg len(*r.myString)


len(*r.myString)



playground example






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Comments

Popular posts from this blog

Executable numpy error

Trying to Print Gridster Items to PDF without overlapping contents

Mass disable jenkins jobs