String Formatting in JavaScript
// JavaScript doesn't have a built-in struct type, so we'll use an object
const point = {
x: 1,
y: 2
};
// JavaScript offers several ways to format strings. Here are some examples:
// Basic object formatting
console.log(`struct1: ${point}`);
// Object with property names
console.log(`struct2: ${JSON.stringify(point)}`);
// Object representation similar to source code
console.log(`struct3: ${JSON.stringify(point, null, 2)}`);
// Type of a value
console.log(`type: ${typeof point}`);
// Formatting booleans
console.log(`bool: ${true}`);
// Formatting integers
console.log(`int: ${123}`);
// Binary representation
console.log(`bin: ${(14).toString(2)}`);
// Character from integer
console.log(`char: ${String.fromCharCode(33)}`);
// Hexadecimal representation
console.log(`hex: ${(456).toString(16)}`);
// Formatting floats
console.log(`float1: ${78.9}`);
// Scientific notation
console.log(`float2: ${(123400000.0).toExponential()}`);
console.log(`float3: ${(123400000.0).toExponential().toUpperCase()}`);
// String formatting
console.log(`str1: ${"string"}`);
// Quoted string
console.log(`str2: "${"string"}"`);
// Hexadecimal representation of a string
console.log(`str3: ${Buffer.from("hex this").toString('hex')}`);
// Pointer representation (not applicable in JavaScript)
console.log(`pointer: N/A in JavaScript`);
// Formatting with width (right-justified)
console.log(`width1: |${12.toString().padStart(6)}|${345.toString().padStart(6)}|`);
// Formatting floats with width and precision
console.log(`width2: |${1.2.toFixed(2).padStart(6)}|${3.45.toFixed(2).padStart(6)}|`);
// Left-justified formatting
console.log(`width3: |${1.2.toFixed(2).padEnd(6)}|${3.45.toFixed(2).padEnd(6)}|`);
// String formatting with width (right-justified)
console.log(`width4: |${"foo".padStart(6)}|${"b".padStart(6)}|`);
// Left-justified string formatting
console.log(`width5: |${"foo".padEnd(6)}|${"b".padEnd(6)}|`);
// String interpolation (similar to Sprintf)
const s = `sprintf: a ${"string"}`;
console.log(s);
// Writing to stderr (similar to Fprintf)
console.error(`io: an ${"error"}`);This JavaScript code demonstrates various string formatting techniques that are similar to the Go example. Here are some key differences and explanations:
JavaScript doesn’t have a built-in struct type, so we use a plain object instead.
JavaScript uses template literals (backticks) for string interpolation, which is similar to Printf in Go.
For more complex formatting, we often use methods like
toString(),toFixed(),padStart(), andpadEnd().JavaScript doesn’t have a direct equivalent to Go’s
%#vfor printing a source code representation of a value. We useJSON.stringify()with indentation as an approximation.JavaScript doesn’t have pointers, so that example is omitted.
For binary and hexadecimal representations, we use the
toString()method with a radix argument.To get a character from an integer, we use
String.fromCharCode().For scientific notation, we use the
toExponential()method.To get a hexadecimal representation of a string, we use the
Bufferclass (this requires Node.js environment).JavaScript doesn’t have a direct equivalent to
Fprintf, but we can useconsole.error()to write to stderr.
This code provides a JavaScript approach to string formatting that covers most of the functionality demonstrated in the Go example.