如何创建不会被复制且不能为null的函数参数

In golang, Is it possible to create a function that takes a struct with the following constraints:

  • the struct must not be copied (its relatively big)
  • the caller must not be able to pass nil

EDIT: I tried using pointers but that can be set to null. I can't find any good articles on how to use references and it doesn't seem like I can pass by reference.

You can create tiny struct wrapper which holds private pointer to big struct and defines Get method to allow acquisition of this big struct. Inside Get you check if pointer is nil then it panics.

Something like:

type StructHolder struct {
    target *BigStruct
}

func (s StructHolder) Get() *BigStruct {
    if s.target == nil {
        panic("target is nil")
    }

    return s.target
}

Why would you do this? I'd think its better to pass a pointer and check its value.