How to add Span before my <a tag only for main menu 1st level items I need to add a span tag before mt 

if i add like 

function mytheme_link($variables) {
  return '&lt;span&gt;&lt;a href="' . check_plain(url($variables['path'], $variables['options'])) . '"' . drupal_attributes($variables['options']['attributes']) . '&gt;' . ($variables['options']['html'] ? $variables['text'] : check_plain($variables['text'])) . '&lt;/a&gt;&lt;/span&gt;';
}


it will add for all the menus 

and if i use 

function mytheme_menu_link($link) {
  if (empty($link['localized_options'])) {
    $link['localized_options'] = array();
  }

  $link['localized_options']['html'] = TRUE; //let l function know that our link title includes html
  return '&lt;span&gt;'.l($link['title'], $link['href'], $link['localized_options']).'&lt;/span&gt;';
}


it does not add at all.
you have forgotten the '#' before localized_options, title &amp; href. 

function mytheme_menu_link($link) {
  if (empty($link['#localized_options'])) {
    $link['#localized_options'] = array();
  }

  $link['#localized_options']['html'] = TRUE; //let l function know that our link title includes html
  return '&lt;span&gt;'.l($link['#title'], $link['#href'], $link['#localized_options']).'&lt;/span&gt;';
}


for main menue only use theme_menu_link__main_menu($var)