Golang : Read file and convert content to string

Problem :

You want to read a file content and convert the content into string.

Solution :

Read the file with ioutil.ReadFile() function. The file content will be in []byte and you just convert the byte into string

 s := string(filecontent)

Below is a full example :

 package main

 import (

 func main() {

  file, err := ioutil.ReadFile("testfile.txt")

  if err != nil {

  // out the file content


References :



  See also : Golang : Read a file line by line

By Adam Ng

IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.