如何使用base64编码作为输入处理[] byte(字节数组)

I want to send a json string with base64 encoded file in it from a client, basically it looks like this:

{  
   "data":"aGVscA==",
   "filename":"file.txt"
}

And I wrote this struct:

type StoredFile struct {
    Data     []byte `json:"data"`
    Filename string `json:"filename"`
}

Then I decode the json into the struct:

decoder := json.NewDecoder(request.Body)
storedFile := StoredFile{}
err := decoder.Decode(&storedFile)

And save it with gorm:

db.Create(&storedFile)

My question is:

  1. How do json package decode the base64 string to byte array? Does it treat it like plain text, because I know that the data size will be increased by 33% when it is encoded to base64 and if it is treated like text the 33% increase will still be there right?
  2. What kind of encoding does gorm use when it stores []byte to PostgreSQL database? Is it UTF-8? If not, how do I set the encoding to UTF-8?
  1. Yes, it considers your b64 string like plain text, because there's no way it can know that it's a base 64 string. So yes again, your size increase will still be there.

  2. The encoding depends on the table. If gorm is simply writing into an already existing table, it will use the encoding that is specified in the structure. For example:

CREATE TABLE `users` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `email` varchar(255) NOT NULL,
  `password` varchar(255) NOT NULL,
  `token_user_id` varchar(255) NOT NULL,
  `is_admin` tinyint(4) NOT NULL DEFAULT '1',
  PRIMARY KEY (`id`),
  UNIQUE KEY (email)

) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;

For this table, the CHARSET is utf8mb4 so it would store using an UTF-8 encoding.

If gorm is also responsible for creating the table however, I believe it uses utf8 by default as the encoding, but I can't seem to find any source to back it up.