Monday, July 15, 2024
HomeGolangCan pointers personal information in go? - Getting Assist

Can pointers personal information in go? – Getting Assist


Hello there! Very new to go and have a query about pointers.

Was following a tutorial right here: Writing Net Purposes – The Go Programming Language.

This perform form of has me confused :frowning:

func loadPage(title string) (*Web page, error) {
    filename := title + ".txt"
    physique, err := os.ReadFile(filename)
    if err != nil {
        return nil, err
    }
    return &Web page{Title: title, Physique: physique}, nil
}

not related to my query however right here is the web page struct for completeness

kind Web page struct {
    Title string
    Physique  []byte
}

Coming from C++ and Rust, this code appears to have an apparent flaw. It returns a pointer to information created inside the features scope.

In cpp or rust this may result in dangling pointer or simply wouldn’t compile.
Does this pointer “personal” the info it factors to? When the pointer goes out of scope later is the info cleared up?

This results in much more questions. What if I’ve a struct as a variable AND have a pointer to mentioned struct? Which one “owns” that information from a rubbish assortment standpoint?
This playground code makes me suppose they each “personal” it (form of like reference counting)

Any and all perception is actually appreciated :slight_smile:

PS: to be clear I’m extra discovering a go-footgun or nooby mistake than I a am within the internal workings of the go language (data on each is welcome tho :+1:.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments