User Program Communication

Print in Go

Used to output text directly to the console.

fmt.Println("Message") //assuming fmt is imported

fmt.Println("Text variableSpecifier", variableIdentifier)


The 'fmt' package must be imported to print.

Aside from PrintLn, other C-style prints like Printf can be used.

variableSpecifier is used to output a variable in its place, indicated by %. The default variable specifier is '%v', which is a catch-all for most variable types.

Specifiers depend on variable type: %d (decimal), %f (floating-point), %e or %E (floating-point in scientific notation), %g or %G (uses the shorter version of %f or %e / %E), %s (strings).

For floating point numbers, precision can be indicated (the number of decimal points wanted) by adding: %.3f, where 3 represents the number of deicmal points.

To pad a number with zeros, add %03f, where 3 indicates the total number of values. Using %#x (with the x specifier), the number will be converted to hexadecimal.

Multiple specifiers can be used in this order: %[flag][width][.][precision][conversion specifier]. The flag can be: #, 0 (padding), -, + (shifting). The precison determines the max number placements where the width determines the min number placements %4.3f, where 4 represents the width and 3 represents the precision. The conversion specifier indicates the variable type of the output.

package main

import 'fmt'

func main() {
    var name string
    name = "Rob and Ken"
    fmt.Println("Hello, %v", name) //Will print 'Hello, Rob and Ken'

See Also

fmt - The Go Programming Langauge

Add to Slack

< Defer   |   User Input >

© 2018 SyntaxDB. All Rights Reserved.