@@ -38,12 +38,6 @@ fn remote_pkg(name: &str) -> String {
3838 format ! ( r#""github.com/bytecodealliance/wit-bindgen/{name}""# )
3939}
4040
41- /// This is the literal location of the Go package.
42- const REPLACEMENT_PKG : & str = concat ! (
43- "github.com/bytecodealliance/wit-bindgen/crates/go/src/package v" ,
44- env!( "CARGO_PKG_VERSION" )
45- ) ;
46-
4741#[ derive( Default , Debug , Copy , Clone ) ]
4842pub enum Format {
4943 #[ default]
@@ -105,10 +99,6 @@ pub struct Opts {
10599 /// (or "wit_component" if `None`).
106100 #[ cfg_attr( feature = "clap" , clap( long) ) ]
107101 pub mod_name : Option < String > ,
108-
109- /// The package name used in the top-level Wasm exports file (or "main" if `None`).
110- #[ cfg_attr( feature = "clap" , clap( long) ) ]
111- pub pkg_name : Option < String > ,
112102}
113103
114104impl Opts {
@@ -853,18 +843,39 @@ impl WorldGenerator for Go {
853843 . collect :: < Vec < _ > > ( )
854844 . join ( "\n " ) ;
855845
856- // If a package name isn't "main", there isn't a need for a main function.
857- let ( package_name , main_func ) = if let Some ( pkg ) = self . opts . pkg_name . as_deref ( ) {
858- ( pkg , "" )
846+ let ( exports_file_path , package_name , main_func ) = if self . opts . mod_name . is_some ( ) {
847+ // If a module name is specified, the generated files will be used as a library.
848+ ( "wit_exports/wit_exports.go" , "wit_exports" , "" )
859849 } else {
860- let func = r#"// Unused, but present to make the compiler happy
850+ // This is the literal location of the Go package.
851+ let replacement_pkg = concat ! (
852+ "github.com/bytecodealliance/wit-bindgen/crates/go/src/package v" ,
853+ env!( "CARGO_PKG_VERSION" )
854+ ) ;
855+
856+ files. push (
857+ "go.mod" ,
858+ format ! (
859+ "module {}\n \n go 1.25\n \n replace github.com/bytecodealliance/wit-bindgen => {}" ,
860+ self . opts. mod_name. as_deref( ) . unwrap_or( "wit_component" ) ,
861+ replacement_pkg
862+ )
863+ . as_bytes ( ) ,
864+ ) ;
865+
866+ // If a module name is NOT specified, the generated files will be used as a
867+ // standalone executable.
868+ (
869+ "wit_exports.go" ,
870+ "main" ,
871+ r#"// Unused, but present to make the compiler happy
861872func main() {}
862- "# ;
863- ( "main" , func )
873+ "# ,
874+ )
864875 } ;
865876
866877 files. push (
867- "wit_exports.go" ,
878+ exports_file_path ,
868879 & maybe_gofmt (
869880 self . opts . format ,
870881 format ! (
@@ -888,15 +899,6 @@ var {SYNC_EXPORT_PINNER} = runtime.Pinner{{}}
888899 . as_bytes ( ) ,
889900 ) ,
890901 ) ;
891- files. push (
892- "go.mod" ,
893- format ! (
894- "module {}\n \n go 1.25\n \n replace github.com/bytecodealliance/wit-bindgen => {}" ,
895- self . opts. mod_name. as_deref( ) . unwrap_or( "wit_component" ) ,
896- REPLACEMENT_PKG
897- )
898- . as_bytes ( ) ,
899- ) ;
900902
901903 for ( prefix, interfaces) in [ ( "export_" , & self . export_interfaces ) , ( "" , & self . interfaces ) ] {
902904 for ( name, data) in interfaces {
0 commit comments