Go中Java静态属性的等效性

class Array {
    public static int MAX_SIZE = 42;
}
Array arr = new Array();
int size = Array.MAX_SIZE;

So, we can create an object of class Array, and we also have a property of class Array. What will be the equivalence of this code in Go?

Go has no classes. Go has no static variables. So there is no equivalent.

The closest concepts Go has are package constants, and struct fields. Neither is exactly the same as what you're looking for.

It's not a plain equivalent:

package array

type Array struct {}

const MaxSize int = 42

In another place:

package main

import "./array"

fmt.Println(array.MaxSize)

If you attempt to program in Go like you program in Java, your code will be terrible. Forget Java, program in Go. Go does not have classes.

In Go, you might see something like this (it is not a Java class):

package array

const MaxSize = 42

type Array struct {
    slice []byte
}

func New(size int) *Array {
    if size < 0 {
        return nil
    }
    if size > MaxSize {
        size = MaxSize
    }
    return &Array{slice: make([]byte, size)}
}

func (a *Array) Size(size int) int {
    if a == nil {
        return 0
    }
    return len(a.slice)
}