Skip to content

Commit 258b5be

Browse files
committed
feat(go): using the 'mod-name' flag results in a more-intuitive Go package structure
Signed-off-by: Andrew Steurer <[email protected]>
1 parent bb35e59 commit 258b5be

File tree

1 file changed

+22
-20
lines changed

1 file changed

+22
-20
lines changed

crates/go/src/lib.rs

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,6 @@ pub struct Opts {
105105
/// (or "wit_component" if `None`).
106106
#[cfg_attr(feature = "clap", clap(long))]
107107
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>,
112108
}
113109

114110
impl Opts {
@@ -853,18 +849,33 @@ impl WorldGenerator for Go {
853849
.collect::<Vec<_>>()
854850
.join("\n");
855851

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, "")
852+
let (exports_file_path, package_name, main_func) = if self.opts.mod_name.is_some() {
853+
// If a module name is specified, the generated files will be used as a library.
854+
("wit_exports/wit_exports.go", "wit_exports", "")
859855
} else {
860-
let func = r#"// Unused, but present to make the compiler happy
856+
// If a module name is NOT specified, the generated files will be used as a
857+
// standalone executable.
858+
files.push(
859+
"go.mod",
860+
format!(
861+
"module {}\n\ngo 1.25\n\nreplace github.com/bytecodealliance/wit-bindgen => {}",
862+
self.opts.mod_name.as_deref().unwrap_or("wit_component"),
863+
REPLACEMENT_PKG
864+
)
865+
.as_bytes(),
866+
);
867+
868+
(
869+
"wit_exports.go",
870+
"main",
871+
r#"// Unused, but present to make the compiler happy
861872
func 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\ngo 1.25\n\nreplace 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

Comments
 (0)