The chemical reactions and pathways involving glycogen, a polydisperse, highly branched glucan composed of chains of D-glucose residues in alpha-(1->4) glycosidic linkage, joined together by alpha-(1->6) glycosidic linkages.