I have a slice of bytes which contains a json object inclosed in curlybrackets with some leading characters of variable lenght. I need to trim away the leading characters until the first curlybracket is encountered. How can i do this? Below is some code just to give an idea of what i want it to look like, the "somefunction()" is the function i'm hoping someone can tell me about.
var b = []byte("I want this removed {here is some json}")
a := somefunction(b, "{")
fmt.Println(string(a)) // desired output: {here is some json}
Find the first index of the rune '{' in the byte slice and re-slice b
. The bytes package provide such functions:
var b = []byte("I want this removed {here is some json}")
b = b[bytes.IndexRune(b, '{'):]
fmt.Println(string(b))
# {here is some json}
Find the index of the first brace. Then, you can use strings.substring, or you can slice your byte array:
var b = []byte("I want this removed {here is some json}")
i := strings.Index(string(b), "{")
fmt.Println(string(b[i:]))