Tuesday, July 23, 2024
HomeGolangEmbedding with out particular strategies? - Getting Assist

Embedding with out particular strategies? – Getting Assist

I’m studying the present implementation of io.Copy for TCPConn. It has a struct known as tcpConnWithoutWriteTo, which can trigger the WriterTo casting from a io.Reader to fail. How and why does it work internally?

It gained’t fail on casting. It should panic, whenever you attempt to name WriteTo

It should positively not panic, this system will crash each time customers invoke the io.Copy on TCPConn. I’ve a demo applied the identical sample, the forged will simply return nil, false as a substitute of panicking:

bundle fundamental

import "fmt"

kind Fooer interface {

kind Barer interface {

kind base struct {
	msg string

func (b *base) Foo() {
	fmt.Println("Foo", b.msg)

func (b *base) Bar() {
	fmt.Println("Bar", b.msg)

kind noFoo struct{}

kind baseWithNoFoo struct {

func (b *baseWithNoFoo) Foo() {
	panic("Shouldn't be known as")

func test(b Barer) {
	f, okay := b.(Fooer)
	fmt.Println(f, okay)

func fundamental() {
	b := &base{msg: "hiya"}
	bNoFoo := baseWithNoFoo{base: b}

Instance Output:

[u@h d]% go run check.go 
Foo hiya
<nil> false

Okay, I did my instance unsuitable. It gained’t forged. However there’s an error in your instance as effectively. Your panic code ought to belong to noFoo. I believe it’s associated to your digging into TCP and splicing right here. It’s finished this fashion, so io.Copy gained’t forged it both. ReadFrom or commonplace copy algorithm can be used as a substitute.



Please enter your comment!
Please enter your name here

Most Popular

Recent Comments