Skip to content

Commit bb35e59

Browse files
authored
feat(go): add 'pkg-name' flag and update generated filename (#1503)
Signed-off-by: Andrew Steurer <[email protected]>
1 parent 4ca80d0 commit bb35e59

File tree

1 file changed

+11
-15
lines changed

1 file changed

+11
-15
lines changed

crates/go/src/lib.rs

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,10 @@ 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>,
108112
}
109113

110114
impl Opts {
@@ -849,26 +853,18 @@ impl WorldGenerator for Go {
849853
.collect::<Vec<_>>()
850854
.join("\n");
851855

852-
// When a custom module name is provided, the generated bindings use package name
853-
// "wit_component" so they can be imported into another package. Without a custom
854-
// module name, the bindings use package "main" for standalone execution.
855-
let package_name = if self.opts.mod_name.is_some() {
856-
"wit_component"
857-
} else {
858-
"main"
859-
};
860-
861-
// If the package name isn't "main", a main function isn't required.
862-
let main_func = if self.opts.mod_name.is_some() {
863-
""
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, "")
864859
} else {
865-
r#"// Unused, but present to make the compiler happy
860+
let func = r#"// Unused, but present to make the compiler happy
866861
func main() {}
867-
"#
862+
"#;
863+
("main", func)
868864
};
869865

870866
files.push(
871-
"wit_bindings.go",
867+
"wit_exports.go",
872868
&maybe_gofmt(
873869
self.opts.format,
874870
format!(

0 commit comments

Comments
 (0)