@@ -116,6 +116,15 @@ pub struct Opts {
116116 #[ cfg_attr( feature = "clap" , clap( long) ) ]
117117 pub pkg_name : Option < String > ,
118118
119+ /// When `--pkg-name` is specified, optionally specify a different package
120+ /// for exports.
121+ ///
122+ /// This allows you to put the exports and imports in separate packages when
123+ /// building a library. If only `--pkg-name` is specified, this will
124+ /// default to that value.
125+ #[ cfg_attr( feature = "clap" , clap( long, requires = "pkg_name" ) ) ]
126+ pub export_pkg_name : Option < String > ,
127+
119128 /// Print the version of the remote package being used for the shared WIT types.
120129 ///
121130 /// Must be specified in addition to the `pkg-name` flag.
@@ -210,8 +219,12 @@ struct Go {
210219
211220impl Go {
212221 /// Adds the bindings module prefix to a package name.
213- fn mod_pkg ( & self , name : & str ) -> String {
214- let prefix = self . opts . pkg_name . as_deref ( ) . unwrap_or ( "wit_component" ) ;
222+ fn mod_pkg ( & self , for_export : bool , name : & str ) -> String {
223+ let prefix = for_export
224+ . then_some ( ( ) )
225+ . and ( self . opts . export_pkg_name . as_deref ( ) )
226+ . or ( self . opts . pkg_name . as_deref ( ) )
227+ . unwrap_or ( "wit_component" ) ;
215228 format ! ( r#""{prefix}/{name}""# )
216229 }
217230
@@ -236,7 +249,7 @@ impl Go {
236249 package
237250 } ;
238251 let prefix = format ! ( "{package}." ) ;
239- imports. insert ( self . mod_pkg ( & package) ) ;
252+ imports. insert ( self . mod_pkg ( exported , & package) ) ;
240253 prefix
241254 }
242255 }
@@ -883,16 +896,14 @@ impl WorldGenerator for Go {
883896 files. push (
884897 "go.mod" ,
885898 format ! (
886- r#"module {}
899+ r#"module wit_component
887900
888901go 1.25
889902
890903require (
891- go.bytecodealliance.org/pkg {}
904+ go.bytecodealliance.org/pkg {REMOTE_PKG_VERSION }
892905)
893906"# ,
894- self . opts. pkg_name. as_deref( ) . unwrap_or( "wit_component" ) ,
895- REMOTE_PKG_VERSION ,
896907 )
897908 . as_bytes ( ) ,
898909 ) ;
@@ -1778,7 +1789,7 @@ for index := 0; index < int({length}); index++ {{
17781789 FunctionKind :: Freestanding | FunctionKind :: AsyncFreestanding => {
17791790 let args = operands. join ( ", " ) ;
17801791 let call = format ! ( "{package}.{name}({args})" ) ;
1781- self . imports . insert ( self . generator . mod_pkg ( & package) ) ;
1792+ self . imports . insert ( self . generator . mod_pkg ( true , & package) ) ;
17821793 call
17831794 }
17841795 FunctionKind :: Constructor ( ty) => {
@@ -1789,7 +1800,7 @@ for index := 0; index < int({length}); index++ {{
17891800 . unwrap ( )
17901801 . to_upper_camel_case ( ) ;
17911802 let call = format ! ( "{package}.Make{ty}({args})" ) ;
1792- self . imports . insert ( self . generator . mod_pkg ( & package) ) ;
1803+ self . imports . insert ( self . generator . mod_pkg ( true , & package) ) ;
17931804 call
17941805 }
17951806 FunctionKind :: Method ( _) | FunctionKind :: AsyncMethod ( _) => {
0 commit comments