Return array length from receiver function

I’ve an array as:

// Stock file
type Stock []StockLine

And want to get the length of this array as:

//Records returns number of records in the stock file
func (s *Stock) Records() int {
	return len(*s)
}

But the returned output looks to be address instead of the length, i got:

0x1154420

How can I return the proper length?

Your code appears to work.

package main

import "fmt"

type StockLine struct{}

// Stock file
type Stock []StockLine

//Records returns number of records in the stock file
func (s *Stock) Records() int {
	return len(*s)
}

func main() {
	stock := Stock(make([]StockLine, 7))
	fmt.Println(stock.Records())
}
$ go run stock.go
7
$

Provide a simple, reproducible example that gives your result.

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.