开发者

Go: Call a function passing pointer to array

开发者 https://www.devze.com 2023-03-06 18:16 出处:网络
I have spent a bit of time attempting to do this, and I think I need a global array (not slice), and I want to pass it as a pointer, not by value. The function receiving the pointer needs to test for

I have spent a bit of time attempting to do this, and I think I need a global array (not slice), and I want to pass it as a pointer, not by value. The function receiving the pointer needs to test for nil, and if nil, read the array from disk, using eg: "baForm, oOserr = ioutil.ReadFile(sFormName)". The calling function may pass either a global array or an array local to the calling function which I presume will be garbage collected.

The reason for doing this is that I want a standard function to read the forms from disk, and often-used forms are stored globally. Despite whether some may think there is a better way, I still want to know how to achieve this ie: a) have global or local array, b) not pass by value, c) The global arrays will be read only once from disk and the 开发者_开发技巧local arrays will be read each time the function is called. TIA.


From reading your description, I can't see why passing a pointer to an array is any way better than passing a slice -- but it's up to you.

You can pass a pointer just like you do in C -- attach an asterisk (*) to the declaration, and perpend an ampersand (&) to the value when you call the function.

Just remember that in Go, the array size is part of its type. This means that your function declaration will have the array size embedded into it, so you can't call the function using an array of any different size. This reason alone is generally enough to warrant using a slice instead of an array.


Here's a sample program that maintains a dynamic forms buffer based on use count. If the ReadForm function finds a form, it returns the address of the form and a nil error.

package main

import (
    "fmt"
    "io/ioutil"
    "math"
    "os"
    "sync"
)

type Form struct {
    Name     string
    useCount int64
    Data     []byte
}

// The capacity of the forms buffer.
const formsCap = 2

// The forms buffer.
var (
    forms     = make(map[string]*Form, formsCap)
    formsLock sync.RWMutex
)

func ReadForm(name string) (form *Form, err os.Error) {
    formsLock.RLock()
    form, ok := forms[name]
    formsLock.RUnlock()
    if !ok {
            form = &Form{name, 0, nil}
    }
    if form.Data == nil {
        data, err := ioutil.ReadFile(name + ".form")
        if err != nil {
            return nil, err
        }
        form = &Form{name, 0, data}
        formsLock.Lock()
        if len(forms) >= formsCap {
            minForm := &Form{useCount: math.MaxInt64}
            for i, f := range forms {
                if f.useCount < minForm.useCount {
                    minForm = f
                }
            }
            minform.Data = nil
        }
        forms[name] = form
        formsLock.Unlock()
    }
    form.useCount++
    return form, nil
}

func main() {
    // form files are named name.form e.g. form1.form
    for _, name := range []string{"form1", "form2", "form3"} {
        f, err := ReadForm(name)
        if err != nil {
            fmt.Println(err)
        } else {
            fmt.Println(string(f.Data))
        }
    }
    fmt.Println(len(forms), forms)
}

EDIT: Map operations are not atomic. Revise the sample program to use a mutex for concurrent access to the forms map.

0

精彩评论

暂无评论...
验证码 换一张
取 消