github.com/dannin/go@v0.0.0-20161031215817-d35dfd405eaa/misc/cgo/testplugin/src/host/host.go (about)

     1  // Copyright 2016 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package main
     6  
     7  import (
     8  	"fmt"
     9  	"log"
    10  	"path/filepath"
    11  	"plugin"
    12  
    13  	"common"
    14  )
    15  
    16  func init() {
    17  	common.X *= 5
    18  }
    19  
    20  func main() {
    21  	if got, want := common.X, 3*5; got != want {
    22  		log.Fatalf("before plugin load common.X=%d, want %d", got, want)
    23  	}
    24  
    25  	p, err := plugin.Open("plugin1.so")
    26  	if err != nil {
    27  		log.Fatalf("plugin.Open failed: %v", err)
    28  	}
    29  
    30  	const wantX = 3 * 5 * 7
    31  	if got := common.X; got != wantX {
    32  		log.Fatalf("after plugin load common.X=%d, want %d", got, wantX)
    33  	}
    34  
    35  	seven, err := p.Lookup("Seven")
    36  	if err != nil {
    37  		log.Fatalf(`Lookup("Seven") failed: %v`, err)
    38  	}
    39  	if got, want := *seven.(*int), 7; got != want {
    40  		log.Fatalf("plugin1.Seven=%d, want %d", got, want)
    41  	}
    42  
    43  	readFunc, err := p.Lookup("ReadCommonX")
    44  	if err != nil {
    45  		log.Fatalf(`plugin1.Lookup("ReadCommonX") failed: %v`, err)
    46  	}
    47  	if got := readFunc.(func() int)(); got != wantX {
    48  		log.Fatalf("plugin1.ReadCommonX()=%d, want %d", got, wantX)
    49  	}
    50  
    51  	// sub/plugin1.so is a different plugin with the same name as
    52  	// the already loaded plugin. It also depends on common. Test
    53  	// that we can load the different plugin, it is actually
    54  	// different, and that it sees the same common package.
    55  	subpPath, err := filepath.Abs("sub/plugin1.so")
    56  	if err != nil {
    57  		log.Fatalf("filepath.Abs(%q) failed: %v", subpPath, err)
    58  	}
    59  	subp, err := plugin.Open(subpPath)
    60  	if err != nil {
    61  		log.Fatalf("plugin.Open(%q) failed: %v", subpPath, err)
    62  	}
    63  
    64  	readFunc, err = subp.Lookup("ReadCommonX")
    65  	if err != nil {
    66  		log.Fatalf(`sub/plugin1.Lookup("ReadCommonX") failed: %v`, err)
    67  	}
    68  	if got := readFunc.(func() int)(); got != wantX {
    69  		log.Fatalf("sub/plugin1.ReadCommonX()=%d, want %d", got, wantX)
    70  	}
    71  
    72  	subf, err := subp.Lookup("F")
    73  	if err != nil {
    74  		log.Fatalf(`sub/plugin1.Lookup("F") failed: %v`, err)
    75  	}
    76  	if gotf := subf.(func() int)(); gotf != 17 {
    77  		log.Fatalf(`sub/plugin1.F()=%d, want 17`, gotf)
    78  	}
    79  	f, err := p.Lookup("F")
    80  	if err != nil {
    81  		log.Fatalf(`plugin1.Lookup("F") failed: %v`, err)
    82  	}
    83  	if gotf := f.(func() int)(); gotf != 3 {
    84  		log.Fatalf(`plugin1.F()=%d, want 17`, gotf)
    85  	}
    86  
    87  	fmt.Println("PASS")
    88  }