Location of type declarations - go

I am curious if this type of declaration for structs
t := Person{"girlie", 12}
only works if its type is declared in the same file.
Below are my files.
file st.go, type def structure inside to be used in main func
package structs
type person struct {
age int
name int
}
file practice.go, main function:
package main
import(
"fmt"
"structs/dir"
)
func main() {
var s dir.Person
s.Name = "She"
s.Age = 12
>> t := Person{"girlie", 12}
fmt.Println(s.Name)
fmt.Println(t.Name)
}
As you can see an error occurs where instance t is declared.

You're missing the package reference, it should be:
t := dir.Person{"girlie", 12}
// ^^^^ missing this part
Assuming that the line var s dir.Person works, which I'm guessing it does based on the question, which means that your quoted contents of "st.go" are not accurate, because the package name is different, and the struct and its fields are not exported.

Related

How to resolve "use of package without a selector error"

I am using a package: ccs08
In my main, I am importing the package and using some of its functions. I have followed the usage from the unit tests given:
package main
import(
"fmt"
"math/big"
"crypto/rand"
"zkrp/ccs08"
"zkrp/crypto/bn256"
)
func main(){
var (
r *big.Int
s []int64
)
s = make([]int64, 4)
s[0] = 12
s[1] = 42
s[2] = 61
s[3] = 71
p, _ := ccs08.SetupSet(s)
r, _ = rand.Int(rand.Reader, bn256.Order)
proof_out, _ := ccs08.ProveSet(12, r, p)
result, _ := ccs08.VerifySet(&proof_out, &p)
if result != true {
fmt.Println("not verified")
}
}
I then have a similar main file,main2, where I am using another set of functions from the same package. This does NOT work
package main
import(
"fmt"
"math/big"
"crypto/rand"
"zkrp/ccs08"
"zkrp/crypto/bn256"
)
func main(){
var (
result bool
zkrp ccs08
)
zkrp.Setup(347184000, 599644800)
zkrp.x = new(big.Int).SetInt64(419835123)
zkrp.r, _ = rand.Int(rand.Reader, bn256.Order)
e := zkrp.Prove()
result, _ = zkrp.Verify()
if result != true {
fmt.println("not verified")
}
}
I am getting this error:
./main2.go:7: imported and not used: "zkrp/ccs08".
./main2.go:16: use of package ccs08 without selector.
What am I doing wrong?
For the first file, everything works because the code is using public members of the ccs08 package. However, this is not the case for the second file.
You mentioned the code in the second file is copied from here.
To understand why the code does not work when pasted into a main method outside the ccs08 package, you need to understand what it is doing in its original location. Specifically this part:
var (
result bool
zkrp ccs08
)
In the original location (inside the ccs08 package) there is a private struct type named ccs08. The declaration can be seen here. What the above code is doing is creating a variable named zkrp the type of which is the private struct type ccs08. Then, the various functions called on the variable are methods bound to this private struct (you can see the methods here).
Now, in the code you posted (after pasting into a location outside the ccs08 package), there is no way to access the private ccs08 struct (I can't even find a public method which returns it in the zkrp source code). So, what Go sees when it tries to execute this code is a typo (where it thinks you are trying to write something of the form package.Member):
// this looks like you wanted to declare a variable with type:
// "ccs08.SomeType" but forgot to write ".SomeType"
// Thus Go gives the error: "use of package ccs08 without selector"
var zkrp ccs08
And the "imported and not used" error is because the ccs08 package isn't used in a valid way anywhere in the code.
The code which you copied is honestly pretty confusing with all the reusing of the strings ccs08 and zkrp for naming of different things. I hope this clears it up a bit.

Getting "implicit assignment of unexported field"

package main
main.go
import (
"fmt"
"practice/pkg"
)
func main() {
mk := pkg.MustKey{map[string]string{"Hello": "bar"}}
fmt.Printf("%v\n", mk)
}
pkg package
hello.go
package pkg
type MustKey struct {
m map[string]string
}
While executing the following, I am getting error as mentioned in the subject line. Any help will be appreciated.
There is a very important rule in Go - how to Export/unexport any functions/methods/fields.
Export - when the name starts with a Captial letter (say it Public)
unexport - when the name starts with a small letter (say it Private)
So in your case, the struct type name MustKey is exportable (starts with a capital M) and can be accessed outside your defined package pkg. But the map variable m inside the struct does start with a small m, so it cannot be accessed outside the package and private to that package only.
So, you have 2 solutions:
Either use M instead of m, like:
type MustKey struct {
M map[string]string
}
Or, if you still want the map variable private - use Exported methods with helping of interface
type MustKey struct {
m map[string]string
}
func (mk *MustKey) GetValue(key string) (string, error) {
value, ok := m[key]
if !ok {
return "", fmt.Errorf("Key is not available: %s", key)
}
return value, nil
}
func (mk *MustKey) SetValue(key, value string) {
m[key] = value
}
And you can use these Get and Set methods to put your own logic.
Read this for good understanding.
In this code:
type MustKey struct {
m map[string]string
}
the map variable is in lower case so it is un-exported (and only private to that package). In Golang to export any field from one pkg to another it should me in Upper case.
Two solutions:
1) Declare Map fields in Upper case, eg:
type MustKey struct {
// m map[string]string
// Upper case fields can be export to other packages
M map[string]string
}
2) Wrap your structure in one function and export the function name.
MustKey.m is an unexported field. You are attempting to initialize that field without referring to it by name with pkg.MustKey{map[string]string{"Hello": "bar"}}.
You either have to export the field by renaming it to M, or you have to define a constructor function that will set it in the package:
func NewMustKey(m map[string]string) MustKey {
return MustKey{m:m}
}
The field m in the MustKey struct is lower case. Therefore it is an unexported field and cannot be used by a program that imports the pkg package. Unexported fields have to be operated on by methods or functions that are internal to the pkg package. Or change it to an M and then use that externally.
You are implicitly using m when you do the initialization in main.

What's the difference between new(Struct) and &Struct{} in Go?

They seem to be the same:
package main
import "fmt"
type S struct {
i int
}
func main() {
var s1 *S = new(S)
fmt.Println(s1)
var s2 *S = &S{}
fmt.Println(s2) // Prints the same thing.
}
Update:
Hm. I just realized that there's no obvious way to initialize S.i using new. Is there a way to do that? new(S{i:1}) does not seem to work :/
From Effective Go:
As a limiting case, if a composite literal contains no fields at all, it creates a zero value for the type. The expressions new(File) and &File{} are equivalent.
Not only do they give the same resulting value, but if we allocate something both ways and look at their values...
// Adapted from http://tour.golang.org/#30
package main
import "fmt"
type Vertex struct {
X, Y int
}
func main() {
v := &Vertex{}
v2 := new(Vertex)
fmt.Printf("%p %p", v, v2)
}
...we'll see that they are in fact allocated in consecutive memory slots. Typical output: 0x10328100 0x10328108. I'm not sure if this is an implementation detail or part of the specification, but it does demonstrate that they're both being allocated from the same pool.
Play around with the code here.
As for initializing with new, according to the language spec: The built-in function new takes a type T and returns a value of type *T. The memory [pointed to] is initialized as described in the section on initial values. Because functions in go can't be overloaded, and this isn't a variadic function, there's no way to pass in any initialization data. Instead, go will initialize it with whatever version of 0 makes sense for the type and any member fields, as appropriate.
Case 1: package main
import (
"fmt"
)
type Drink struct {
Name string
Flavour string
}
func main() {
a := new(Drink)
a.Name = "Maaza"
a.Flavour = "Mango"
b := a
fmt.Println(&a)
fmt.Println(&b)
b.Name = "Frooti"
fmt.Println(a.Name)
}//This will output Frooti for a.Name, even though the addresses for a and b are different.
Case 2:
package main
import (
"fmt"
)
type Drink struct {
Name string
Flavour string
}
func main() {
a := Drink{
Name: "Maaza",
Flavour: "Mango",
}
b := a
fmt.Println(&a)
fmt.Println(&b)
b.Name = "Froti"
fmt.Println(a.Name)
}//This will output Maaza for a.Name. To get Frooti in this case assign b:=&a.

golang: can i share C.int between packages

in the main package i have:
var foo C.int
foo = 3
t := fastergo.Ctuner_new()
fastergo.Ctuner_register_parameter(t, &foo, 0, 100, 1)
in the fastergo package i have:
func Ctuner_register_parameter(tuner unsafe.Pointer, parameter *C.int, from C.int, to C.int, step C.int) C.int {
...
}
if i try to run it, i get:
demo.go:14[/tmp/go-build742221968/command-line-arguments/_obj/demo.cgo1.go:21]: cannot use &foo (type *_Ctype_int) as type *fastergo._Ctype_int in function argument
i am not really sure what go is trying to tell me here, but somehow i think it wants to tell me, that all C.int are not equal? why is this the case? how can i solve this / work around?
Since _Ctype_int doesn't begin with a Unicode upper case letter, the type is local to the package. Use Go types, except in the C wrapper package where you convert them to C types. The wrapper package should hide all the implementation details.
You don't provide sufficient information for us to create sample code which compiles and runs. Here's a rough outline of what I expected to see:
package main
import "tuner"
func main() {
var foo int
foo = 3
t := tuner.New()
t.RegisterParameter(&foo, 0, 100, 1)
}
.
package tuner
import (
"unsafe"
)
/*
#include "ctuner.h"
*/
import "C"
type Tuner struct {
ctuner uintptr
}
func New() *Tuner {
var t Tuner
t.ctuner = uintptr(unsafe.Pointer(C.ctuner_new()))
return &t
}
func (t *Tuner) RegisterParameter(parameter *int, from, to, step int) error {
var rv C.int
rv = C.ctuner_register_parameter(
(*C.ctuner)(unsafe.Pointer(t.ctuner)),
(*C.int)(unsafe.Pointer(parameter)),
C.int(from),
C.int(to),
C.int(step),
)
if rv != 0 {
// handle error
}
return nil
}
As explained by peterSO, you can't pass C.int between packages. However, you can pass pointers between packages by converting the pointer type. To do this, you would define a named type in the target package, import that type into the calling package and covert via unsafe.Pointer. There isn't any point in doing this with a single int.
However, it is helpful if you keep code to convert complex types in a package; for example an array of strings (or any sort of nested array).
The example below is for exporting a go function to be called in C, but this works in reverse, ie. if you want to call a C functions which a returns nested array.
package convert
import "C"
type PP_char **C.char
func From_c_to_go(arr_str PP_char, length int) []string {
// Some operation on the Ctype
var slice []string
for _, s := range unsafe.Slice(arr_str, length) {
if s == nil {
break
}
x := C.GoString(s)
slice = append(slice, x)
}
return slice
}
package main
import "C"
import "convert"
//export myFunc
func myFunc(arr_str **C.char, length int){
retyped_arr_str := convert.PP_char(unsafe.Pointer(arr_str))
slice := convert.From_c_to_go(retyped_arr_str, length)
// Do something with slice
}
You could instead decide to pass instance of unsafe.Pointer as an argument to the go function in the target package and perform the type conversion in that function.

Using reflect, how do you set the value of a struct field?

having a rough time working with struct fields using reflect package. in particular, have not figured out how to set the field value.
type t struct { fi int; fs string }
var r t = t{ 123, "jblow" }
var i64 int64 = 456
getting Name of field i - this seems to work
var field = reflect.TypeOf(r).Field(i).Name
getting value of field i as a) interface{}, b) int - this seems to work
var iface interface{} = reflect.ValueOf(r).Field(i).Interface()
var i int = int(reflect.ValueOf(r).Field(i).Int())
setting value of field i - try one - panic
reflect.ValueOf(r).Field(i).SetInt( i64 )
panic: reflect.Value·SetInt using value obtained using unexported field
assuming it did not like field names "id" and "name", so renamed to "Id" and "Name"
a) is this assumption correct?
b) if correct, thought not necessary since in same file / package
setting value of field i - try two (with field names capitalized ) - panic
reflect.ValueOf(r).Field(i).SetInt( 465 )
reflect.ValueOf(r).Field(i).SetInt( i64 )
panic: reflect.Value·SetInt using unaddressable value
Instructions below by #peterSO are thorough and high quality
Four. this works:
reflect.ValueOf(&r).Elem().Field(i).SetInt( i64 )
he documents as well that the field names must be exportable (begin with capital letter)
The Go json package marshals and unmarshals JSON from and to Go structures.
Here's a step-by-step example which sets the value of a struct field while carefully avoiding errors.
The Go reflect package has a CanAddr function.
func (v Value) CanAddr() bool
CanAddr returns true if the value's
address can be obtained with Addr.
Such values are called addressable. A
value is addressable if it is an
element of a slice, an element of an
addressable array, a field of an
addressable struct, or the result of
dereferencing a pointer. If CanAddr
returns false, calling Addr will
panic.
The Go reflect package has a CanSet function, which, if true, implies that CanAddr is also true.
func (v Value) CanSet() bool
CanSet returns true if the value of v
can be changed. A Value can be changed
only if it is addressable and was not
obtained by the use of unexported
struct fields. If CanSet returns
false, calling Set or any
type-specific setter (e.g., SetBool,
SetInt64) will panic.
We need to make sure we can Set the struct field. For example,
package main
import (
"fmt"
"reflect"
)
func main() {
type t struct {
N int
}
var n = t{42}
// N at start
fmt.Println(n.N)
// pointer to struct - addressable
ps := reflect.ValueOf(&n)
// struct
s := ps.Elem()
if s.Kind() == reflect.Struct {
// exported field
f := s.FieldByName("N")
if f.IsValid() {
// A Value can be changed only if it is
// addressable and was not obtained by
// the use of unexported struct fields.
if f.CanSet() {
// change value of N
if f.Kind() == reflect.Int {
x := int64(7)
if !f.OverflowInt(x) {
f.SetInt(x)
}
}
}
}
}
// N at end
fmt.Println(n.N)
}
Output:
42
7
If we can be certain that all the error checks are unnecessary, the example simplifies to,
package main
import (
"fmt"
"reflect"
)
func main() {
type t struct {
N int
}
var n = t{42}
fmt.Println(n.N)
reflect.ValueOf(&n).Elem().FieldByName("N").SetInt(7)
fmt.Println(n.N)
}
BTW, Go is available as open source code. A good way to learn about reflection is to see how the core Go developers use it. For example, the Go fmt and json packages. The package documentation has links to the source code files under the heading Package files.
This seems to work:
package main
import (
"fmt"
"reflect"
)
type Foo struct {
Number int
Text string
}
func main() {
foo := Foo{123, "Hello"}
fmt.Println(int(reflect.ValueOf(foo).Field(0).Int()))
reflect.ValueOf(&foo).Elem().Field(0).SetInt(321)
fmt.Println(int(reflect.ValueOf(foo).Field(0).Int()))
}
Prints:
123
321

Resources