B
B
bqio2019-07-02 19:30:35
go
bqio, 2019-07-02 19:30:35

Why can't I access the method?

package main

import (
  "bytes"
  "encoding/binary"
  "fmt"
  "io/ioutil"
  "log"
)

type Buffer struct {
  *bytes.Buffer
}

func (buf *Buffer) readUInt32() uint32 {
  b := make([]byte, 4)
  buf.Read(b)
  return binary.LittleEndian.Uint32(b)
}

func check(err error) {
  if err != nil {
    log.Fatal(err)
  }
}

func main() {
  content, err := ioutil.ReadFile("subtitle.pck")
  check(err)
  buf := bytes.NewBuffer(content)
  fmt.Print(buf.readUInt32()) // buf.readUInt32 undefined (type *bytes.Buffer has no field or method readUInt32)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
bqio, 2019-07-02
@bqio

package main

import (
  "bytes"
  "fmt"
  "io/ioutil"
  "log"
)

type Buffer struct {
  bytes.Buffer
}

func (buf *Buffer) readUInt32() uint32 {
  b := make([]byte, 4)
  buf.Read(b)
  return binary.LittleEndian.Uint32(b)
}

func check(err error) {
  if err != nil {
    log.Fatal(err)
  }
}

func main() {
  var buf Buffer

  content, err := ioutil.ReadFile("subtitle.pck")
  check(err)

  buf.Write(content)

  fmt.Print(buf.readUInt32())
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question