# fmt.Printf/Println does not work outside of a block

**URL:** <https://forum.golangbridge.org/t/fmt-printf-println-does-not-work-outside-of-a-block/14799>\
**Category:** Getting Help\
**Created:** [August 5, 2019, 6:11pm UTC](https://forum.golangbridge.org/t/fmt-printf-println-does-not-work-outside-of-a-block/14799 "2019-08-05T18:11:18Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![pritesh](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/pritesh/32/5970_2.png) [@pritesh](https://forum.golangbridge.org/u/pritesh)\
**Post date:** [August 5, 2019, 6:11pm UTC](https://forum.golangbridge.org/t/fmt-printf-println-does-not-work-outside-of-a-block/14799/1 "2019-08-05T18:11:18Z")

</div>

Hi,

I tried printing the following:

```auto
package main

import "fmt"

var i int = 20 
fmt.Println(i)

func main() {
	var i int = 10
	fmt.Printf("i Value: %d\ti address: %p\n", i, &i)
}

```

I get an error stating the following:

```auto
main.go|6 col 1| : expected declaration, found fmt
main.go|10 col 2| : expected declaration, found fmt

```

I am trying to print the value and address of the variable `i`  
While I know that the `i` in the main is different than the `i` in the main, printing it out is something that’s not working. If the `i` in the main is commented, then it works well.  
What am I missing? Is `fmt.Println` or `fmt.Printf` not designed to work outside of a block? Or am I missing something fundamental ?

---

<div class="post-metadata">

**Author:** ![Yamil\_Bracho](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/yamil_bracho/32/5141_2.png) [@Yamil\_Bracho](https://forum.golangbridge.org/u/Yamil_Bracho)\
**Post date:** [August 5, 2019, 6:15pm UTC](https://forum.golangbridge.org/t/fmt-printf-println-does-not-work-outside-of-a-block/14799/2 "2019-08-05T18:15:36Z")

</div>

The only sentences allowed outside a function block are declarations of variables, definition of types and constants

---

<div class="post-metadata">

**Author:** ![pritesh](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/pritesh/32/5970_2.png) [@pritesh](https://forum.golangbridge.org/u/pritesh)\
**Post date:** [August 5, 2019, 6:16pm UTC](https://forum.golangbridge.org/t/fmt-printf-println-does-not-work-outside-of-a-block/14799/3 "2019-08-05T18:16:13Z")

</div>

Hi Yamil,  
Thanks for the update. How did you get so much knowledge? Did you go through any specific book or online course? Or just practice and experience?

---

<div class="post-metadata">

**Author:** ![NobbZ](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/nobbz/32/3096_2.png) [@NobbZ](https://forum.golangbridge.org/u/NobbZ)\
**Post date:** [August 5, 2019, 6:21pm UTC](https://forum.golangbridge.org/t/fmt-printf-println-does-not-work-outside-of-a-block/14799/4 "2019-08-05T18:21:26Z")

</div>

Well, that’s how _most_ compiled languages work. Also you can read this from the language specification if you are used to BNF in general.

[https://golang.org/ref/spec](https://golang.org/ref/spec)

---

<div class="post-metadata">

**Author:** ![Yamil\_Bracho](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/yamil_bracho/32/5141_2.png) [@Yamil\_Bracho](https://forum.golangbridge.org/u/Yamil_Bracho)\
**Post date:** [August 5, 2019, 6:31pm UTC](https://forum.golangbridge.org/t/fmt-printf-println-does-not-work-outside-of-a-block/14799/5 "2019-08-05T18:31:53Z")

</div>

Well, The best way to learn a new language is to write code and see the code of other colleagues… 🙂  
And do not forget to reads the docs:ok\_hand:

---

<div class="post-metadata">

**Author:** ![pritesh](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/pritesh/32/5970_2.png) [@pritesh](https://forum.golangbridge.org/u/pritesh)\
**Post date:** [August 5, 2019, 6:54pm UTC](https://forum.golangbridge.org/t/fmt-printf-println-does-not-work-outside-of-a-block/14799/6 "2019-08-05T18:54:39Z")

</div>

Hi,  
I had done some C programming but don’t remember much. But thanks for pointing this out.

---

<div class="post-metadata">

**Author:** ![Yamil\_Bracho](https://forum.golangbridge.org/user_avatar/forum.golangbridge.org/yamil_bracho/32/5141_2.png) [@Yamil\_Bracho](https://forum.golangbridge.org/u/Yamil_Bracho)\
**Post date:** [August 5, 2019, 8:32pm UTC](https://forum.golangbridge.org/t/fmt-printf-println-does-not-work-outside-of-a-block/14799/7 "2019-08-05T20:32:41Z")

</div>

That’s a good starting…

---

<div class="post-metadata">

**Author:** ![system](https://forum.golangbridge.org/uploads/default/original/2X/5/5ad234f7c3dbc68b6cc72d338ce4f2973d9fd81c.png) [@system](https://forum.golangbridge.org/u/system)\
**Post date:** [November 3, 2019, 8:44pm UTC](https://forum.golangbridge.org/t/fmt-printf-println-does-not-work-outside-of-a-block/14799/8 "2019-11-03T20:44:10Z")

</div>

Closed on November 3, 2019, 8:44pm UTC

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
