Logo

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
Categories
4 categories in total
go
open
learning
open
notes
open
notion
open
Author
9 person written this
gaurav444
open
Learning GO : 08 - File Handling, Error Handling

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 called WriteFile 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)
}
Enter fullscreen mode Exit fullscreen mode

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")
Enter fullscreen mode Exit fullscreen mode
  • 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 the string value and bitSize which will be 32 or 64
func readFile() float64 {
    data, _ := os.ReadFile("balance.txt")
    balanceText := string(data)
    balance, _ := strconv.ParseFloat(balanceText, 64)
    return balance
}
Enter fullscreen mode Exit fullscreen mode
  • 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 in errors 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")
    }

Enter fullscreen mode Exit fullscreen mode
    var accountBalance, err = readFile()

    if err != nil {
        fmt.Println("ERROR")
        fmt.Println(err)
        fmt.Println("===============")
    }
Enter fullscreen mode Exit fullscreen mode

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!")
    } 
Enter fullscreen mode Exit fullscreen mode
notion Article's
30 articles in total
Favicon
Why Most Students Struggle with Organization (And How Notion Templates Can Help)
Favicon
Why Your Notion Templates Arenโ€™t Selling (And How to Fix It)
Favicon
Extract metadata of paper and export it to Notion DB
Favicon
Organize Your Gifts Effortlessly with This Notion Template ๐ŸŽ
Favicon
Why Most Notion Templates Fail Businesses (And How to Create Ones That Work)
Favicon
Un blog statique sur mesure avec Notion headless
Favicon
Learning GO : 09 - Packages
Favicon
Notion is making a super customizable email app
Favicon
Project Planning
Favicon
Learning GO : 08 - File Handling, Error Handling
Favicon
Learning GO: 07 - Loops
Favicon
Learning GO: 06 - Function return values, if condition
Favicon
Learning GO: 05 - Function Declaration
Favicon
I Analyzed 4 Successful Products to Find These Powerful UI Design and Implementation Principles
Favicon
How I Sync my Obsidian Notes For Free !
Favicon
Best Notion Time Tracking Software
Favicon
Using Notion as a CMS and create a personal blog with React
Favicon
How to Embed Live Reports in Notion Using Flowtrail AI
Favicon
3+ Productivity Hacks with Notion
Favicon
Building a personal blog with Next.js and Notion
Favicon
Notion for NextJS CMS
Favicon
Count everything in your Notion workspace
Favicon
The Best Notion Alternatives in 2024
Favicon
Install Notion on Ubuntu
Favicon
Free Notion Style Avatar Generator
Favicon
Discover Your Notion Page Insights with Notion Statistics
Favicon
Bot Telegram + Notion
Favicon
Building a Notion-style activity feed with Next.js and shadcn/ui
Favicon
I switched from Notion to Obsidian
Favicon
Launch your startup in 450+ communities

Featured ones: