Avatar ·

How to read an entire file into a string variable in Go?

Hello! I just started learning Go and encountered a seemingly simple task: I need to read the entire contents of a text file into a variable of type `string`. In other languages, this is done with a single function, but in Go I got confused. I tried this: ```go package main import ( "fmt" "os" ) func main() { file, err := os.Open("data.txt") if err != nil { fmt.Println("Error opening:", err) return } // What next? How to get a string? file.Close() } ``` I saw that there is a package `ioutil`, but in recent versions of Go it seems to be deprecated. I tried reading via `bufio` — only got one line. I also tried `os.ReadFile`, but I'm not sure if that's correct. Please advise how to correctly and idiomatically read an entire file into a string in modern Go.