fix composition is not inheretance
This commit is contained in:
@@ -0,0 +1,3 @@
|
|||||||
|
module gitea.zacksolutions.dev/Zakaria/compositions
|
||||||
|
|
||||||
|
go 1.27.1
|
||||||
@@ -0,0 +1,44 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"path/filepath"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Pair struct {
|
||||||
|
Path string
|
||||||
|
Hash string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p Pair) String() string {
|
||||||
|
return fmt.Sprintf("Hash of %s is %s", p.Path, p.Hash)
|
||||||
|
}
|
||||||
|
|
||||||
|
type PairWithLength struct {
|
||||||
|
Pair
|
||||||
|
Length int
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p PairWithLength) String() string {
|
||||||
|
return fmt.Sprintf("Hash of %s is %s; length %d", p.Path, p.Hash, p.Length)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p Pair) Filename() string {
|
||||||
|
return filepath.Base(p.Path)
|
||||||
|
}
|
||||||
|
|
||||||
|
type Filenamer interface {
|
||||||
|
Filename() string
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
p := Pair{"/urs", "0xfdfe"}
|
||||||
|
pl := PairWithLength{Pair{"/usr/bin", "0xdead"}, 13643532}
|
||||||
|
|
||||||
|
var fn Filenamer = PairWithLength{Pair{"/usr/bin", "0xdead"}, 13643532}
|
||||||
|
|
||||||
|
fmt.Println(p)
|
||||||
|
fmt.Println(fn)
|
||||||
|
fmt.Println(pl.Path)
|
||||||
|
fmt.Println(p.Filename())
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user