Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 34 additions & 10 deletions crates/go/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -775,6 +775,9 @@ impl WorldGenerator for Go {
fn finish(&mut self, resolve: &Resolve, id: WorldId, files: &mut Files) -> Result<()> {
_ = (resolve, id);

let version = env!("CARGO_PKG_VERSION");
let header = &format!("// Generated by `wit-bindgen` {version}. DO NOT EDIT!");

let src = mem::take(&mut self.src);
let align = self.return_area_align.format(POINTER_SIZE_EXPRESSION);
let size = self.return_area_size.format(POINTER_SIZE_EXPRESSION);
Expand All @@ -792,7 +795,8 @@ impl WorldGenerator for Go {
&maybe_gofmt(
self.opts.format,
format!(
r#"package main
r#"{header}
package main

import (
"runtime"
Expand All @@ -815,7 +819,7 @@ func main() {{}}
files.push("go.mod", b"module wit_component\n\ngo 1.25");
files.push(
"wit_runtime/wit_runtime.go",
include_bytes!("wit_runtime.go"),
format!("{header}\n{}", include_str!("wit_runtime.go")).as_bytes(),
);

for (prefix, interfaces) in [("export_", &self.export_interfaces), ("", &self.interfaces)] {
Expand All @@ -828,7 +832,8 @@ func main() {{}}
&maybe_gofmt(
self.opts.format,
format!(
"package {prefix}{name}
"{header}
package {prefix}{name}

import (
{imports}
Expand Down Expand Up @@ -869,7 +874,8 @@ import (
&maybe_gofmt(
self.opts.format,
format!(
r#"package wit_types
r#"{header}
package wit_types

{tuples}
"#
Expand All @@ -880,27 +886,45 @@ import (
}

if self.need_async {
files.push("wit_async/wit_async.go", include_bytes!("wit_async.go"));
files.push(
"wit_async/wit_async.go",
format!("{header}\n{}", include_str!("wit_async.go")).as_bytes(),
);
}

if self.need_option {
files.push("wit_types/wit_option.go", include_bytes!("wit_option.go"));
files.push(
"wit_types/wit_option.go",
format!("{header}\n{}", include_str!("wit_option.go")).as_bytes(),
);
}

if self.need_result {
files.push("wit_types/wit_result.go", include_bytes!("wit_result.go"));
files.push(
"wit_types/wit_result.go",
format!("{header}\n{}", include_str!("wit_result.go")).as_bytes(),
);
}

if self.need_unit {
files.push("wit_types/wit_unit.go", include_bytes!("wit_unit.go"));
files.push(
"wit_types/wit_unit.go",
format!("{header}\n{}", include_str!("wit_unit.go")).as_bytes(),
);
}

if self.need_future {
files.push("wit_types/wit_future.go", include_bytes!("wit_future.go"));
files.push(
"wit_types/wit_future.go",
format!("{header}\n{}", include_str!("wit_future.go")).as_bytes(),
);
}

if self.need_stream {
files.push("wit_types/wit_stream.go", include_bytes!("wit_stream.go"));
files.push(
"wit_types/wit_stream.go",
format!("{header}\n{}", include_str!("wit_stream.go")).as_bytes(),
);
}

Ok(())
Expand Down
Loading