4

Following this tutorial and the github repo I understood the use of plugins.

The tutorial compiles each file separately into so files.

go build -buildmode=plugin -o eng/eng.so eng/greeter.go
go build -buildmode=plugin -o chi/chi.so chi/greeter.go

How can I merge two files into a single .so file? I tried following command by separating files through space

go build -buildmode=plugin -o bin/langs.so src/test/eng/greeter.go src/test/chi/greeter.go

The error:

named files must all be in one directory; have src/test/eng/ and src/test/chi/

The idea is to have a single .so files from different packages.

Edit: I guess the follow up question would be how to combine all .so files into one archive if one has several packages of a library and go only allows one .so file per package.

Developer
  • 924
  • 3
  • 14
  • 30

1 Answers1

1

You can't put them in different folders because they should have same package name (main). But you can put them in different files like this :

file1:

package main

import "fmt"

type greeting_en string

func (g greeting_en) Greet() {
    fmt.Println("Hello Universe")
}


var GreeterEn greeting_en

file2:

package main

import "fmt"

type greeting_chi string


func (g greeting_chi) Greet() {
    fmt.Println("你好宇宙")
}


var GreeterChi greeting_chi

compile them like this :

go build -buildmode=plugin -o ./langs.go 

And load langs like this :

mod = "./langs.so"
plug, _ := plugin.Open(mod)
EnglishGreeter,_ := plug.Lookup("GreeterEn")
ChineseGreeter,_ := plug.Lookup("GreeterChi")
Mostafa Solati
  • 1,235
  • 2
  • 13
  • 33
  • so essentially it's one `so` file per package, so how's it done in real life? I've a library of multiple packages, that means I'll be distributing several `so` files for my project? is there an archive that allows to combine all so files in a package? – Developer Apr 27 '19 at 18:55
  • No there is no such thing you have to combine them yourself – Mostafa Solati Apr 28 '19 at 03:21
  • is this applicable -> https://stackoverflow.com/questions/915128/merge-multiple-so-shared-libraries – Developer Apr 28 '19 at 03:24