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:

  1. JavaScript doesn’t have a built-in struct type, so we use a plain object instead.

  2. JavaScript uses template literals (backticks) for string interpolation, which is similar to Printf in Go.

  3. For more complex formatting, we often use methods like toString(), toFixed(), padStart(), and padEnd().

  4. JavaScript doesn’t have a direct equivalent to Go’s %#v for printing a source code representation of a value. We use JSON.stringify() with indentation as an approximation.

  5. JavaScript doesn’t have pointers, so that example is omitted.

  6. For binary and hexadecimal representations, we use the toString() method with a radix argument.

  7. To get a character from an integer, we use String.fromCharCode().

  8. For scientific notation, we use the toExponential() method.

  9. To get a hexadecimal representation of a string, we use the Buffer class (this requires Node.js environment).

  10. JavaScript doesn’t have a direct equivalent to Fprintf, but we can use console.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.