Checking length of a string pointer in Go


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
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.
String pointers are always the same size--the size of a pointer.
– Flimzy
2 days ago