Master file handling and I/O operations in Go with these 30 multiple-choice questions. Explore reading and writing files, working with CSV, JSON, XML, and error handling in file operations.
file, err := os.Open("file.txt")file := os.Open("file.txt")file, err := os.Create("file.txt")file := os.ReadFile("file.txt")os.Create("file.txt")os.NewFile("file.txt")os.Open("file.txt")os.WriteFile("file.txt")os.Open function in Go?file.Read()file.Scan()file.Write()file.Open()file.Write([]byte("Hello World"))file.Append([]byte("Hello World"))file.WriteString("Hello World")file.WriteText("Hello World")file.Close()file.End()file.Finish()file.Exit()if err != nil {}if file.isErr() {}if err.exists() {}if file.hasError() {}os.ReadFile function do in Go?nilscanner := bufio.NewScanner(file)file.ReadLine()file.ReadLines()scanner := os.NewScanner(file)encoding/csvcsvencoding/jsonos/csvcsv.Writer(file).Write()csv.Append(file)csv.Open(file)csv.WriteFile(file)csv.NewReader(file).ReadAll()csv.Open(file)csv.Read(file)csv.ReadFile(file)json.Marshal(data)json.Encode(data)json.Write(data)json.Convert(data)json.Unmarshal()json.Decode()json.Deserialize()json.Parse()json.NewEncoder(file).Encode(data)json.WriteFile(file, data)json.MarshalToFile(file, data)json.Write(file, data)xml.Unmarshal do in Go?xml.NewEncoder(file).Encode(data)xml.WriteFile(file, data)xml.Marshal(data)xml.Write(file, data)encoding/xmlxmlos/xmlxmlparserjson.Marshal(data) in Go?reader.Read()scanner.Scan()csv.ReadLine()csv.GetRow()json.RawMessageomitempty tag in struct fieldsjson.Unmarshalfile.Close()csv.Close()csv.End()file.End()json.MarshalIndent()json.Pretty()json.Print()json.Indent()json.Marshal(array)json.Encode(array)json.ToJSON(array)json.ConvertToJSON(array)os.OpenFile do in Go?ioutil.ReadFile()os.ReadFile()file.ReadAll()file.Read()encoding/xml.Marshal do in Go?json.NewDecoder(file).Decode(&data)json.Read(file, data)json.Unmarshal(file, data)json.Decode(file, data)| Qno | Answer (Option with Text) |
|---|---|
| 1 | a) file, err := os.Open("file.txt") |
| 2 | a) os.Create("file.txt") |
| 3 | a) A file pointer and an error |
| 4 | a) file.Read() |
| 5 | a) file.Write([]byte("Hello World")) |
| 6 | a) file.Close() |
| 7 | a) if err != nil {} |
| 8 | a) It reads the entire content of a file and returns it as a byte slice |
| 9 | a) It will return an error |
| 10 | a) scanner := bufio.NewScanner(file) |
| 11 | a) encoding/csv |
| 12 | a) csv.Writer(file).Write() |
| 13 | a) csv.NewReader(file).ReadAll() |
| 14 | a) json.Marshal(data) |
| 15 | a) json.Unmarshal() |
| 16 | a) json.NewEncoder(file).Encode(data) |
| 17 | a) It converts XML data into Go data structures |
| 18 | a) xml.NewEncoder(file).Encode(data) |
| 19 | a) encoding/xml |
| 20 | a) It converts Go data structures into JSON |
| 21 | a) reader.Read() |
| 22 | b) By using the omitempty tag in struct fields |
| 23 | a) file.Close() |
| 24 | a) json.MarshalIndent() |
| 25 | a) json.Marshal(array) |
| 26 | a) It opens a file with specific permissions |
| 27 | a) ioutil.ReadFile() |
| 28 | a) It converts Go data structures into XML |
| 29 | a) json.NewDecoder(file).Decode(&data) |
| 30 | d) All of the above |