Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Semrush - keyword research tool

Create your online store today with Shopify

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Disclosure: My content contains affiliate links.

43,624 questions

56,911 answers

573 users

How to format a string using fixed-width fields in Go

6 Answers

0 votes
/*
Common width specifiers
---------------------------------
%-10s // Left-aligned string, width 10
%10s // Right-aligned string, width 10
%5d // Integer, width 5
%08d // Integer, width 8, padded with zeros
%12.2f // Float, width 12, precision 2
*/

package main

import (
	"fmt"
)

// formatRecord formats data into fixed-width columns.
//
// %-15s : left-aligned string, width 15
// %10d  : right-aligned integer, width 10
// %12.2f: right-aligned float, width 12,
//         with 2 digits after the decimal point
func formatRecord(name string, age int, salary float64) string {
	return fmt.Sprintf(
		"%-15s%10d%12.2f",
		name,
		age,
		salary,
	)
}

func main() {
	// Store the formatted result in a variable
	result := formatRecord("Abby", 32, 98000.0)

	fmt.Println(result)
}



/*
run:

Abby                   32    98000.00

*/

 



answered 3 days ago by avibootz
edited 3 days ago by avibootz
0 votes
package main

import "fmt"

// Left-aligned string field

func main() {
	// Create a string field that is 10 characters wide
	result := fmt.Sprintf("%-10s", "Hello")

	fmt.Printf("|%s|\n", result)
}


/*
run:

|Hello     |

*/

 



answered 3 days ago by avibootz
0 votes
package main

import "fmt"

// Right-aligned string field

func main() {
	// Right-align a string in a field 10 characters wide
	result := fmt.Sprintf("%10s", "Hello")

	fmt.Printf("|%s|\n", result)
}


/*
run:

|     Hello|

*/

 



answered 3 days ago by avibootz
0 votes
package main

import "fmt"

// Right-aligned integer field

func main() {
	// Right-align an integer in a field 8 characters wide
	result := fmt.Sprintf("%8d", 123)

	fmt.Printf("|%s|\n", result)
}


/*
run:

|     123|

*/

 



answered 3 days ago by avibootz
0 votes
package main

import "fmt"

// Zero-padded integer

func main() {
	// Pad an integer with leading zeros
	result := fmt.Sprintf("%08d", 123)

	fmt.Println(result)
}


/*
run:

00000123

*/

 



answered 3 days ago by avibootz
0 votes
package main

import "fmt"

// Formatting a table

func main() {
	// Print table header
	fmt.Printf("%-15s%-10s%-12s\n",
		"Name",
		"Age",
		"Salary",
	)

	// Print table rows
	fmt.Printf("%-15s%-10d%-12.2f\n",
		"Ana",
		39,
		80000.0,
	)

	fmt.Printf("%-15s%-10d%-12.2f\n",
		"Andra",
		47,
		121000.0,
	)
}


/*
run:

Name           Age       Salary      
Ana            39        80000.00    
Andra          47        121000.00   

*/

 



answered 3 days ago by avibootz
...