dev-resources.site
for different kinds of informations.
Learning GO : 08 - File Handling, Error Handling
Published at
10/15/2024
Categories
go
learning
notes
notion
Author
gaurav444
Author
9 person written this
gaurav444
open
Hey! I am Currently learning Go Lang, and I am taking some basic Notes on my Notion and though I'd also just publish them here. They are not well thought out or well written but it's just me taking notes from time to time for my reference.
I am taking the Udemy Course by Maximilian Schwarzmรผller,
Notes
Writing in Files
- the
os package
provides a function calledWriteFile
that takes in the name of the package, the data to be stored in the file and file mode, which will be the file modifying permissions.
func writeBalanceFiles(balance float64) {
balanceText := fmt.Sprint(balance)
os.WriteFile("balance.txt", []byte(balanceText), 0644)
}
Reading from a file
- sometimes when we are getting 2 return values from a function, we can then use a special variable name
_
that means we want get the value but we dont want to use it.
data, _ := os.ReadFile("balance.txt")
- here the data will come with a
byte
type, so to handle that, we can only convert that data into a string - we need the data string in float number, so for that we can use the
strconv
package, that gives us different methods to handle string operations - we can use
ParseFloat
method, which will convert the string into floating number, with that we will need to provide thestring value
andbitSize
which will be 32 or 64
func readFile() float64 {
data, _ := os.ReadFile("balance.txt")
balanceText := string(data)
balance, _ := strconv.ParseFloat(balanceText, 64)
return balance
}
- byte values cannot be directly converted into any type other then string and string value directly cannot be converted into any other type, that will require the use of
strconv
package
Error handling
- In GO, there is a special type as
error
that can be used to give out custom errors which comes from the built inerrors package
- In GO, we can use
nil
to check the null value - almost all packages provide an error as return value with a main value, we use the error to to check if there is
nil
value - So, if the error is not nil that means there is an error and in that case we can give out some error response
if err != nil {
return 1000, errors.New("failed to find balance file")
}
var accountBalance, err = readFile()
if err != nil {
fmt.Println("ERROR")
fmt.Println(err)
fmt.Println("===============")
}
Panic!
- there is an in built method called
panic()
that will stop the program execution and give out special error
if err != nil {
fmt.Println("ERROR")
fmt.Println(err)
fmt.Println("===============")
panic("Can't Continue Sorry!")
}
notes Article's
30 articles in total
vim_notes
read article
2024
read article
Exploring Oracle 24D: A Deep Dive into Release Notes
read article
Combining Ghost and Quarto (The Lazy Way)
read article
Managing To Dos in recurring meetings, when there's no agreed upon tool
read article
Google Workspace Mail Management
read article
Google Workspace Security Part 2: SSO, API Access, Managing Connected Apps and the Alert Center
read article
Google Workspace Security Part 1: Common Security Settings and 2SV
read article
Managing Google Workspace Part 3: MDM Basics, Vault, Reporting and Domains
read article
Get 10th Class Math Notes (Matric Part 2) โ Download Now for Free
read article
Managing Google Workspace Part 1: Core Services, Additional Services and Marketplace Apps
read article
Managing Google Workspace Part 2: Gmail, Calendar and Drive
read article
Learning GO : 09 - Packages
read article
Hopefully Helpful Notes, Best Practices, ...
read article
Learning GO : 08 - File Handling, Error Handling
currently reading
Learning GO: 07 - Loops
read article
Learning GO: 03 - User Input, Variable Declaration without initialization
read article
Learning GO: 02 - Variable Declaration
read article
Learning GO: 04 - Printing Strings
read article
Learning GO: 06 - Function return values, if condition
read article
Learning GO : 01 - Type Declaration
read article
Learning GO: 05 - Function Declaration
read article
Why Obsidian Falls Short as a Note-Taking Tool
read article
VS Code for Note-Taking
read article
Notes on The Every Computer Performance Book
read article
dossi is now open source
read article
Promises in JS(Personal notes)
read article
I switched from Notion to Obsidian
read article
Notes templates
read article
Cloud Computing - "It's MAGIC"!
read article
Featured ones: