Home > OS >  How can i remove base64 header with goolang using regex?
How can i remove base64 header with goolang using regex?

Time:04-03

Hi i want to remove a base64 header from a base64 string, for exemple: data:video/mp4;base64,TWFuIGlzIGRpc3Rpbmd1aXNoZWQsIG5vdCBvbmx5IGJ5IGhpcyByZWFzb24sIGJ1dCBieSB0aGlz

to remove: data:video/mp4;base64,

The problem is that i receive different types of video, so i don't know a way to use regex in go to remove this header. Someone can help me?

Thanks

CodePudding user response:

Cut the string at the comma to get the base64 data:

func decode(uri string) ([]byte, error) {
  if !strings.HasPrefix(uri, "data:") {
    return nil, errors.New("not a data uri")
  }
  _, data, ok := strings.Cut(uri, ",")
  if !ok {
    return nil, errors.New("not a data uri")
  }
  return base64.URLEncoding.DecodeString(data)
}
  •  Tags:  
  • go
  • Related